Jump to content

C# random number question


Recommended Posts

I did try a google search as well as visual c# express' built in help search and I came up with what you found,

 private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}

but when I call it later,

int a = RandomNumber(0,9);
int b = RandomNumber(0,9);

a == b every time. I can perform a little math on a to make it different, but I thought there must be a better way to do this.

Thanks for the link.

Lawless%20government%20sig.png
Link to comment
Share on other sites

ok, the problem is you should only use "new Random()" once in your program. Do this during the initialization of your program--in your constructor.

When you need a new random number, just call the Next function.

Basically, by using 'new' you are seeding the random number generator. If you call it again too soon, you are probably seeding it with the same number (I believe the current time is used as the seed). Therefore you will get the same series of random numbers.

Click to play:
j.pngs.pngd.pnga.pngp.png
Download: BoltBait's Plugin Pack | CodeLab | and how about a Computer Dominos Game

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...