简体   繁体   English

C#随机数问题

[英]C# random number problem

I have this code which should generate numbers between 1 and 100: 我有此代码应生成1到100之间的数字:

 int aux; aux = Game1.rand.Next(101); if (aux <= 20) { Trace.WriteLine(aux); seeker = true; } 

The problem is i get values smaller than 20 every time. 问题是我每次都得到小于20的值。 If i change 20 to 30 in the if statement, i always get numbers smaller or equal to 30. How can i overcome this isue? 如果我在if语句中将20更改为30,则我总是得到小于或等于30的数字。如何克服这个问题? Thank you. 谢谢。

You need to put your Trace.WriteLine(aux); 您需要放置Trace.WriteLine(aux); before the if statement in order for it to write out any numbers above 20 (or 30, as the case may be): if语句之前 ,以便写出大于20(或30,视情况而定)的任何数字:

int aux;
aux = Game1.rand.Next(101);
Trace.WriteLine(aux);

if (aux <= 20)
{
  seeker = true;
  //...
}

Try something like 尝试类似

int aux;
aux = Game1.rand.Next(101);
Trace.WriteLine(aux);

You get values below or equal 20 simply because you have if() clause exactly saying you need values below or equal 20. 您得到的值小于或等于20仅仅是因为您拥有if()子句,确切地说您需要的值小于或等于20。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM