开发者

C# random number problem

开发者 https://www.devze.com 2023-02-24 01:44 出处:网络
I have this code which should generate numbers between 1 and 100: int aux; aux = Game1.rand.Next(101); if (aux <= 20)

I have this code which should generate numbers between 1 and 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. 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? 开发者_JAVA技巧Thank you.


You need to put your 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):

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消