Board index » cppbuilder » rand() and randomize() and srand()

rand() and randomize() and srand()


2003-11-04 07:08:48 AM
cppbuilder41
A long time ago I asked a question similar to this and was told to never
call the randomize more than once when using the rand function. I don't
remember the exact reasoning behind something about increasing the likely
hood that randomness wouldn't appear random if it was called. Any comments
or corrections to this statment are appreciated.
I'm in the middle of developing a black jack game to our current medieval
role playing game. Anyone can host a game and up to 4 others can join the
game. The one hosting the game is responsible for the dealer as well as his
own cards. Here's my question. Lets say 4 others are playing the game.
The host generates a seed (how this is done will be determined
later...probably using the system clock). It then passes this seed along to
the other 4 clients. This seed is used to determine the order of the cards.
Each time a card is given to the client or the dealer it simply calls the
rand function. The idea behind this is to minimize the amount of messages
being sent to everyone since if they all have the same seed the order of the
cards will be none by all playing. After everyone's hand has been played
the host will generate a new seed and pass it on to all clients again.
In essense this is basically like calling the randomize function more than
once. What are the results on the random numbers? Are certain card pairs
more likely to be seen when calling the randomize after each hand?
Thanks,
 
 

Re:rand() and randomize() and srand()

"JunkMail" < XXXX@XXXXX.COM >, haber iletisinde žunlar?
yazd?3fa6e000$ XXXX@XXXXX.COM ...
Quote
In essense this is basically like calling the randomize function more than
once. What are the results on the random numbers? Are certain card pairs
more likely to be seen when calling the randomize after each hand?

Thanks,
A file with random generated bytes can't be compressed to a smaller size.
If you are not sure about randomness of the sequence you can try to write
the sequence to a file and then compress it.
 

Re:rand() and randomize() and srand()

Quote
A file with random generated bytes can't be compressed to a smaller size.
If you are not sure about randomness of the sequence you can try to write
the sequence to a file and then compress it.
My question had nothing to do with compression...it had to do with
randomness of the numers being generated.
 

{smallsort}

Re:rand() and randomize() and srand()

"JunkMail" < XXXX@XXXXX.COM >, haber iletisinde žunlar?
yazd? XXXX@XXXXX.COM ...
Quote

>A file with random generated bytes can't be compressed to a smaller
size.
>If you are not sure about randomness of the sequence you can try to
write
>the sequence to a file and then compress it.

My question had nothing to do with compression...it had to do with
randomness of the numers being generated.

If you can compress a file to a smaller size, the contents of the file is
definitely not randomly distributed.
For example a .bmp file can be compressed into a smaller size because the
contents of the file (pixels) is related to each other.(the value of R,G,B
components of the color of a pixel are very close to the value of R,G,B
components of the color of an another pixel located near)
Therefore, your question is related to compression.
Create an array with size 10000, fill it with random numbers, write the
array to a file,
compress the file, you will see that the file cannot be compressed to a
smaller size if your randomizing algorithm works well.
 

Re:rand() and randomize() and srand()

Not sure if this helps, but I have a class that assigns random
ID numbers to objects. I noticed that every time I ran the
test program the sequence of numbers came out the same. I tried
a few things and it made it a little more random, but I was then
getting sets of numbers close to those 5 times ago etc. So this
is what I initialize with now, and it is in the constructor of
a class that is only used one time as a static member, therefore
this is only called one time:
std::srand(time(NULL));
std::srand((time(NULL) % 100) * (rand() / 100));
It tends to give a pretty random seed; I haven't seen even close
to the same sequence of numbers twice. I've noticed a pretty
broad distribution throughout the range of rand() also.
ta0kira
 

Re:rand() and randomize() and srand()

Quote
compress the file, you will see that the file cannot be compressed to a
smaller size if your randomizing algorithm works well.
I thought about this for a minute, and decided that the only
reason you would not be able to compress it is if numbers were
not duplicated. As you get toward 10k you are reducing the
number of possibilities for future numbers in that case, and are
therefore making it slightly more predictable. A less
predictable algoritm may allow duplicate numbers once in a while
which would make it MORE random. If you do know of an algorithm
that does not give duplicate numbers please let me know because
I have a work-around that checks new rand() against a table of
previous numbers each time a new one is taken, and if it is a
duplicate I try with another rand() until I get a unique one.
Thanks
ta0kira
 

Re:rand() and randomize() and srand()

"JunkMail" < XXXX@XXXXX.COM >wrote:
Quote
I'm in the middle of developing a black jack game to our
current medieval role playing game. Anyone can host a game
and up to 4 others can join the game. The one hosting the
game is responsible for the dealer as well as his own cards.
Here's my question. Lets say 4 others are playing the game.
The host generates a seed (how this is done will be determined
later...probably using the system clock). It then passes this
seed along to the other 4 clients. This seed is used to
determine the order of the cards. Each time a card is given to
the client or the dealer it simply calls the rand function.
The idea behind this is to minimize the amount of messages
being sent to everyone since if they all have the same seed
the order of the cards will be none by all playing.
There is nothing wrong about using srand more than once. Using
the time to create the seed is good in your case because due to
user interaction there will be no fixed interval between two
calls (as it might be in a tight loop for example). I suppose
randomize uses the clock, too, thus the warning not to use it
repeatedly, targets the same problem and is no issue for you.
Well, I guess I don't really have to mention it, but just in
case you haven't thought about it yet: rand() or random() will
produce duplicate values which you must be aware of. I suggest
to use std::random_shuffle() to avoid having to deal with that.