开发者

C# random number problem

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜