Thread: Random Number Generator Always Generates 0 First

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    61

    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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Did you call srand() at the start of main() ?

    Not to mention that there are 52 cards in a deck, not 54.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Quote Originally Posted by Salem View Post
    Did you call srand() at the start of main() ?
    Whoops, no. Thanks for the help.

    Quote Originally Posted by Salem View Post
    Not to mention that there are 52 cards in a deck, not 54.
    There are 54 with Jokers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  2. random number generator help
    By mayoussa89 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2010, 07:26 AM
  3. Replies: 2
    Last Post: 12-25-2003, 01:31 AM
  4. help with random number generator
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-27-2003, 03:46 PM
  5. Random number generator
    By Caze in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2002, 08:10 AM