# Random Number Generator Always Generates 0 First

• 06-21-2011
LyTning94
Random Number Generator Always Generates 0 First
I'm writing a program which generates every number from 0 to 53 in a random order without repeats (shuffling and dealing a full card deck). Every time I run the program, however, it always generates 0 as the first number. Here's my code:

Code:

```void DealCards() {         int RandNum;         bool Dealt[54];         int i;         PlayerTypes ePlayer = (PlayerTypes)((Dealer + 1) % 4);         GamePlayers* pPlayer;                 for (i = 0; i < 54; i++)         {                 Dealt[i] = false;         }         for (i = 0; i < 54; i++)         {                 RandNum = ((float)rand()/(RAND_MAX + 1)) * 54;                 pPlayer = GetPlayer(ePlayer);                 if (Dealt[RandNum])                 {                         i--;                 }                 else                 {                         for (int j = 0; j < 14; j++)                         {                                 if (pPlayer->Hand[j] == -1)                                 {                                         pPlayer->Hand[j] = (CardTypes)RandNum;                                         break;                                 }                         }                         Dealt[RandNum] = true;                         ePlayer = (PlayerTypes)((ePlayer + 1) % 4);                 }         } }```
Any idea what's going on?
• 06-21-2011
Salem
Did you call srand() at the start of main() ?

Not to mention that there are 52 cards in a deck, not 54.
• 06-21-2011
LyTning94
Quote:

Originally Posted by Salem
Did you call srand() at the start of main() ?

Whoops, no. Thanks for the help.

Quote:

Originally Posted by Salem
Not to mention that there are 52 cards in a deck, not 54.

There are 54 with Jokers.
• 06-21-2011
Elysia
I'd suggest you avoid using magic numbers in your code. In this case, I am referring to the size of your array which you continuously put out as 54 all over the code.
What happens if you change the size of that array at some point? Lots of bugs. Possibly dangerous ones.