crosswalker Posted November 15, 2006 Share Posted November 15, 2006 I need to generate two unique random numbers(a,, between 0 & 9, using C#. How would I go about this? Everything I've tried so far has given me only one random number. Quote Link to comment Share on other sites More sharing options...
BoltBait Posted November 15, 2006 Share Posted November 15, 2006 Post up your code and I'll see if I can help. BTW, did you ask Google? http://www.c-sharpcorner.com/Code/2004/ ... Number.asp Quote Click to play: Download: BoltBait's Plugin Pack | CodeLab | and how about a Computer Dominos Game Link to comment Share on other sites More sharing options...
crosswalker Posted November 15, 2006 Author Share Posted November 15, 2006 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. Quote Link to comment Share on other sites More sharing options...
BoltBait Posted November 15, 2006 Share Posted November 15, 2006 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. Quote Click to play: Download: BoltBait's Plugin Pack | CodeLab | and how about a Computer Dominos Game Link to comment Share on other sites More sharing options...
crosswalker Posted November 15, 2006 Author Share Posted November 15, 2006 Oh, ok, Thanks! I'll try that Update: It worked!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.