Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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!

 

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;

  • Administrators
Posted

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...

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;
       }

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Leaders
Posted

The first code listing was:

byte[] randomData = new byte[deck.Length[b][color="Red"][i]-1[/i][/color][/b]];

The corrected code was

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.

[sIGPIC]e[/sIGPIC]
Posted
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.

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...