# Problem with Random Number Generation

## Recommended Posts

quophyie    122
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(){

srand((unsigned)time(0));
int random_integer;
int lowest=1, highest=10;

for(int index=0; index<20; index++){
int *range = new int;
*range=(highest-lowest)+1;
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
createPack();

//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 = deckOfCards.at(i);
deckOfCards.at(i) = deckOfCards.at(randNum);
deckOfCards.at(randNum) = temporary;
}



##### Share on other sites
DaBono    1496
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 on other sites
ToohrVyk    1595
As already mentioned, only call srand once per program.

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

##### Share on other sites
quophyie    122
Thanks guys. It all makes sense now!!

##### Share on other sites
Zahlman    1682
Quote:
 Original post by ToohrVykAlso, 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 on other sites
quophyie    122
Thanks dude. Any ideas and suggestions that I can learn from are very much appreciated.