对于我当前的项目,我创建了一个事件,该事件应该根据我的代码生成的随机整数而改变,唯一的问题是我似乎总是得到相同的路径.总之,我希望它发生任何事件的几率为50%.谢谢,西蒙
random1 = rand() % 1 + 0;
if (random1 == 0) {
int choice4;
cout << "Your character screams at the top of his lungs, " << endl;
cout << "this causes the dragon to immediately to bow in fear..." << endl;
cout << "It turns out dragons are very sensitive to hearing....." << endl;
system("pause");
cout << "\nIt seems the dragon is requesting you ride it!\n" << endl;
cout << "Will you ride it?\n" << endl;
cout << …Run Code Online (Sandbox Code Playgroud) 我的数据库包含问题,每个问题都有三个选项。一切正常,但是当我尝试随机获取问题和选项时,它给了我错误:
我发现了与我的问题类似的问题,但它也没有起作用,
这是我的代码:
public List<Test> getTest() {
List<Test> MyList = new ArrayList<Test>();
myDB=this.getReadableDatabase();
Cursor cursor = myDB.rawQuery("SELECT * FROM " + My_Table + "ORDER BY RANDOM() LIMIT 1", null);
if (cursor.moveToFirst()) {
do {
Test newQ = new Test();
newQ .setId(cursor.getInt(0));
newQ .setTest(cursor.getString(1));
newQ .setAns(cursor.getString(2));
newQ .setO1(cursor.getString(3));
newQ .setO2(cursor.getString(4));
newQ .setO3(cursor.getString(5));
MyList .add(newQ );
} while (cursor.moveToNext());
}
return MyList ;}
Run Code Online (Sandbox Code Playgroud)
当我将此行添加到我的代码中时:
ORDER BY RANDOM()LIMIT 1
该活动将无法进行。
你会添加什么来使它仍然选择一个随机数而不是数字3?
- (IBAction)Button3 {
{
int randomviews = rand() % 6;
Label1.text = [NSString stringWithFormat:@"%i", randomviews];
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试生成这样的随机字符:
char* test = "myname" + GenerateRandomChar();
Run Code Online (Sandbox Code Playgroud)
我正在尝试不使用字符串.所以请不要说使用字符串.谢谢
下面的方法有问题.我得到的当前错误是"它不返回任何值"如果我将返回数字放在括号之外,它在当前上下文中不存在.如果我一起删除括号,我得到:
嵌入式语句不能是声明或带标签的语句当前上下文中不存在名称"rnd"
在没有这个问题之前我已经做过多种方法.我错过了什么?
public static int generateNumber(int timesUserWantsToGuess)
{
for (int i = 0; i <= timesUserWantsToGuess; i++)
{
Random rnd = new Random();
int num = rnd.Next(1, 50);
return num;
}
}
Run Code Online (Sandbox Code Playgroud) 在这段代码中,我试图将0到9之间的随机数添加到一个数组中但是当我试图在for循环中为数组分配数字时,我收到此错误:
错误1使用未分配的局部变量'x'
这是代码:
using System;
class Core
{
public static void Main()
{
Random rnd = new Random();
int[] x;
for (int i = 0; i < 4; i++)
{
x[i] = rnd.Next(1, 9);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了编译器错误CS0165的 MSDN描述,但它没有谈论数组.
所以我试图创建一个"真正的"随机函数,因为兰德不够随意imo.
您是否认为获得随机数是一种"好"方式?如果不是,为什么?
int realrand() {
double duration = 0;
int i = 0;
std::clock_t start;
start = std::clock();
while(duration < 0.001) {
i++;
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
}
int rand = i % 10;
return rand;
}
Run Code Online (Sandbox Code Playgroud) 我试图将一个随机整数传递给构造函数.
hotels[2] = new Hotel(random.nextInt(10)+30, random.nextInt()+100,
random.nextInt()+20, random.nextInt()+200,
random.nextInt()+10, random.nextInt()+300);
Run Code Online (Sandbox Code Playgroud)
但是,在DataPoint构造函数中,内部传递的值是荒谬的,即
Params 1, 3, 5: 38 -695807826 -2078518332
Run Code Online (Sandbox Code Playgroud)
越来越-695807826没有意义.这个数字应该在20到40之间,因为random.nextInt()+20
我使用Random类生成任何随机整数,但它总是返回相同的数字
static Random rand = new Random();
public static int GetOrderID()
{
return rand.Next(Math.Abs(int.MinValue + 1), int.MaxValue);
}
Run Code Online (Sandbox Code Playgroud)
注意随机类是静态的并且在函数外部生成
有一些方法,如搜索重复项,但我想知道是否有更好的解决方案来完成这项任务.