Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random number generator - 2

Status
Not open for further replies.

merlinv

Programmer
Jun 11, 2001
32
US
I would like to use a better random number generator in dealing bridge hands. When I use rand , I don't get uniform selection. It tends to use some numbers more than others.

For example - I would like a number returned between 1-52.

The max_rand is about 32000+. The number returned is between 0-max_rand. If you mod it with 52 it is not uniform.

Any suggestions?

Thanks.

Merlinv
 

Personally, I'd start with say
Code:
Card deck[52];

Then call rand() many times over to exchange
Code:
swap( deck[i], deck[j] );
This is called shuffling (like you do with a real deck)

Then you deal the cards in 0, 1, 2, 3, ... order without lots of tedious problems like trying to extract the last few cards because it takes too long for rand() to come up with a number you haven't guessed before.

--
 
I would do as Salem suggests, but use random_shuffle from <algorithm> to shuffle the "cards" in the array or vector. The shuffling code has already been written for you. Note that you still must seed with srand() for random_shuffle to be different for each program run.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top