Not so random..?

Jibrohni

Freshman
Joined
Apr 15, 2010
Messages
28
Hey all,

I'm trying to generate a random array of 52 integers.

My code below works, but doesn't seem to be very random.

Now coming from VB I know there was a Randomize() function that helped with this, but I don't know how to achieve the same result in C#.

Please help!

Code:
private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }
        public int[] GenerateDeck()
        {
            int[] DECK = new int[52];       //creates an array to hold thegenerated deck
            int[] holdDECK = new int[52];
            int iArrayLength = 52;
            int iNextLocation = 0;

            //initialise tempDECK [1,2,3...52]
            for (int counter = 1; counter < 53; counter++)
            {
                holdDECK[counter - 1] = counter;
            }

            do
            {
                int rndNum = RandomNumber(0, iArrayLength);     //generate random number between 1 and tDECK.Length
               
                // Find the next empty spot
                while (DECK[iNextLocation] != 0)
                { // Go until we find a zero (empty)
                    iNextLocation++;
                }

                DECK[iNextLocation] = holdDECK[rndNum];     //shuffle down at this point

                do
                {
                    holdDECK[rndNum] = holdDECK[rndNum + 1];
                    rndNum++;

                } while ((rndNum + 1) < iArrayLength);

                holdDECK[iArrayLength-1] = 0;

                iArrayLength--;

            } while (iArrayLength > 0);


            return DECK;
 
One thing I would do is move the line Random random = new Random();
out of the RandomNumber method as creating a new instance of the random number generator for each number is overkill. In fact given how the function is nothing more than a call to Random.Next() anyway I would probably remove the method entirely.

In fact personally I would probably simplify the routine and use the built in Array.Sort and the random number generator combined to do all the heavy lifting here.

Try the following and see if it does the job...
Code:
public int[] GenerateDeck()
        {
            Random random = new Random();
            int[] deck = new int[52];
            for (int i = 0; i <= deck.Length-1; i++)
                deck[i] = i;

            byte[] randomData = new byte[deck.Length-1];
            random.NextBytes(randomData);
            Array.Sort(randomData, deck);

            return deck;
        }
 
Thank you very much, however with your method I noticed that the last number 51 is always in the last position at the end.
 
Oops - my bad, caught out by an "off by one error" again.
Try
Code:
byte[] randomData = new byte[deck.Length];
for the array declaration and it should work properly this time.

Sorry ;)
 
Last edited:
The first code listing was:
Code:
byte[] randomData = new byte[deck.Length[B][COLOR="Red"][I]-1[/I][/COLOR][/B]];
The corrected code was
Code:
byte[] randomData = new byte[deck.Length];

Confusing bounds with length is probably the most common cause of off-by-one errors, which is what it appears PD did.
 
Haha, I appreciate your help. C# is almost completely foreign to me at the mo. I haven't done it in ages. I got lazy and decided to use manly VB. MISTAKE.
 
Back
Top