随机不工作

Aar*_*ron 1 c# random

为了澄清,多个学生对象和所有对象都获得了相同的价值.

我知道之前已经问过这个问题,但我对其他关于他主题的帖子没有运气.我有一个1-3的随机数发生器.我然后我们%2使bool值为true或false.每次我运行程序时,我要么全部都是真的,要么都是假的.这是我的代码.我知道随机并不是随机的.我该怎么做才能获得更多随机数.

Random random = new Random();

public Student()
{
    int randomLevel=random.Next(1,3);
    level = (randomLevel % 2 == 0);
}

public bool readingLevel()//this always returns one value for the entire program.
{
    return level;
}
Run Code Online (Sandbox Code Playgroud)

And*_*lon 7

您只是在构造函数中将一个随机值赋值给'level',因此它始终具有初始值.尝试:

public bool readingLevel()
{
     return (random.Next(1,3) % 2 == 0);
}
Run Code Online (Sandbox Code Playgroud)

编辑:

Static Random random = new Random();
...
Run Code Online (Sandbox Code Playgroud)