ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

c# assignment: Problem with random

(1/2) > >>

mediaguycouk:
I'm trying to pick two random numbers for assignment 5 of the c# school. I'm using the following code


--- Code: C# ---private void fillFirstBoxes()        {            userCard1Text.Text = Convert.ToString(functions.GetRandomCard());            dealerCard1Text.Text = Convert.ToString(functions.GetRandomCard());        } ...     public class Functions    {        private int RandomNumber(int min, int max)        {            Random random = new Random();            return random.Next(min, max);        }         public int GetRandomCard()        {            int[] cards = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };            int random = RandomNumber(0, 12);            return cards[random];        }    }
The trouble is that 90% of the time the two numbers are the same. Occasionally (about 10% of the time) the numbers are different but it is rare. So what am I doing wrong?

I know that computer random isn't really random, but surely it only takes a tick?

mouser:
i'm not much of a c# person but something must be wrong.  do you need to randomize the random number generator?

mwb1100:
You're creating a new Random number generator each time you call RandomNumber() - you want to use the same generator for the whole random sequence.  I can't test this out right now because I'm traveling, but if you change the RandomNumber() method to something like:



--- Code: C# ---private int RandomNumber(int min, int max){    static Random random = new Random();    return random.Next(min, max);}
You'll get a single instance of the Random generator that gets used for the entire random number sequence.

rht341:
Hello,

You need to supply the Random constructor with a seed value.  See http://msdn2.microsoft.com/en-us/library/ctssatww.aspx for examples.

I'm trying to pick two random numbers for assignment 5 of the c# school. I'm using the following code


--- Code: C# ---private void fillFirstBoxes()        {            userCard1Text.Text = Convert.ToString(functions.GetRandomCard());            dealerCard1Text.Text = Convert.ToString(functions.GetRandomCard());        } ...     public class Functions    {        private int RandomNumber(int min, int max)        {            Random random = new Random();            return random.Next(min, max);        }         public int GetRandomCard()        {            int[] cards = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };            int random = RandomNumber(0, 12);            return cards[random];        }    }
The trouble is that 90% of the time the two numbers are the same. Occasionally (about 10% of the time) the numbers are different but it is rare. So what am I doing wrong?

I know that computer random isn't really random, but surely it only takes a tick?

-mediaguycouk (March 19, 2008, 05:21 PM)
--- End quote ---

mwb1100:
The trouble is that 90% of the time the two numbers are the same. Occasionally (about 10% of the time) the numbers are different but it is rare. So what am I doing wrong?
-mediaguycouk (March 19, 2008, 05:21 PM)
--- End quote ---

What's happening is that each time you call "new Random()" you are getting a new sequence of random numbers that are seeded by whatever the .NET framework uses by default (probably a timer).  Since you're getting the new random sequences one after the other very quickly, you're getting multiple random number sequences that have the same seed, and therefore have the same sequence of numbers (and you're only getting the first number from that sequence each time).

If you use just a single instance of the Random class, it will be seeded a single time (by default using the timer or whatever) and each time the Random.Next() method is called on that instance you'll get the next random number in the sequence.

I think that using the default seed is appropriate in this case, so you don't need to mess with seeding the generator (you're already doing that whether you know it or not) - you just need to seed the generator once (which happens in the "new Random()" call) and use that generator instead of creating a new generator each time.

Navigation

[0] Message Index

[#] Next page

Go to full version