Sign in to follow this  

Problem with Random Number Generation

Recommended Posts

Hi Guys. I'm having a few problems with some random number generation. My random numbers dont seem to be ramdom at all cos everytime that I call the function to generate random numbers, it seems to spew out the same numbers the random numbers in the same order e.g. if I call the function three times and say the first set of random numbers are , on the second call of the function the same set of random numbers are returned in the same order i.e. 2,5, 11 and this also happens on the 3rd call. Is there someting wrong with my machine? I've tried two different codes and they seem to do the same thing. I got the code of the internet code 1:
int RandNumGen::repeatedRandNumGen(){
	    int random_integer; 
	    int lowest=1, highest=10; 
	    for(int index=0; index<20; index++){ 
	    	  int *range = new int;
	        random_integer = lowest+int(*range*rand()/(RAND_MAX + 1.0)); 
	        cout << random_integer<<" "; 
	    delete range;
	  //  srand(0);

	    return 0;

Code 2:
void Deck::shuffle(){
	// create the pack of cards
	//Shuffle the cards
	int i;
	// Note that deckOfCards is vector of ordered cards. I'm trying to
        //shuffle the ordered cards
    for (i = 0; i < 52; i++)
          int randNum;
          pack::cardType temporary;
          randNum = rand( ) % 52;
          temporary =;
 = temporary;

Share this post

Link to post
Share on other sites
You should call srand only once in your program, instead of calling it every time you generate a set of numbers. The time-function gives the time in seconds, meaning that if you call srand within the same second, you also will seed the RNG with the same seed, and hence get the same numbers.

Share this post

Link to post
Share on other sites
As already mentioned, only call srand once per program.

Also, std::random_shuffle(deckOfCards.begin(), deckOfCards.end());

Share this post

Link to post
Share on other sites
Original post by ToohrVyk
Also, std::random_shuffle(deckOfCards.begin(), deckOfCards.end());

Quoted for emphasis. The code to shuffle things is already written - it would be foolish not to take advantage of that.

Also, that algorithm gives a properly random shuffle; yours does not. Simple proof: consider a three card deck. There are 27 possible ways to pick a random card out of three, three times (three to the third power) and 6 possible ways to order the deck (three factorial). 27 doesn't divide evenly into 6, so some orderings must be favoured over others. A common error :) The way to make it work is to always select the card to swap to be after the one you've iterated to (alternately, always after).

Also, creating the cards has nothing to do with the process of shuffling a deck, so it doesn't belong as a part of the shuffle() function. Instead, a deck should always have cards in it, so make the cards as a part of the process of making the deck. That is, don't have a createPack() function at all; create the cards in the constructor.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this