- #36
- 22,183
- 3,325
Also, it is possible that the rand() is subpar. For example, see this part of the C/C++ manual: http://man7.org/linux/man-pages/man3/rand.3.html
So I suggest a few changes to really make sure we randomized everything and that we don't produce a sequence that repeats itself over and over again. First, use the random function instead of rand.
Second, you have used the code "rand()%6;".
Apparently, using "rand()%2" might give problems, a good replacement for that is "random() & (1<<13)".
So please use the "random() & (1 << 13)" to ouput 0 and 1. Using this outputs, you can generate the dice as follows:
1) Draw 3 random numbers ##x##, ##y##, ##z## using the code above (so three numbers either 0 and 1)
2) Form the number ##a = 1 + x + 2y + 4z##
3) If ##a\leq 6##, then output ##a## as our random dice roll, in the other case, go to (1) again
This should give a good sequence of random dice rolls. You should test it by generating some 100 values and see if it works.
http://cseweb.ucsd.edu/~dasgupta/103/5a.pdf
http://cseweb.ucsd.edu/~dasgupta/103/5b.pdf
NOTES
The versions of rand() and srand() in the Linux C Library use the same
random number generator as random(3) and srandom(3), so the lower-order
bits should be as random as the higher-order bits. However, on older
rand() implementations, and on current implementations on different
systems, the lower-order bits are much less random than the higher-
order bits. Do not use this function in applications intended to be
portable when good randomness is needed. (Use random(3) instead.)
So I suggest a few changes to really make sure we randomized everything and that we don't produce a sequence that repeats itself over and over again. First, use the random function instead of rand.
Second, you have used the code "rand()%6;".
Apparently, using "rand()%2" might give problems, a good replacement for that is "random() & (1<<13)".
So please use the "random() & (1 << 13)" to ouput 0 and 1. Using this outputs, you can generate the dice as follows:
1) Draw 3 random numbers ##x##, ##y##, ##z## using the code above (so three numbers either 0 and 1)
2) Form the number ##a = 1 + x + 2y + 4z##
3) If ##a\leq 6##, then output ##a## as our random dice roll, in the other case, go to (1) again
This should give a good sequence of random dice rolls. You should test it by generating some 100 values and see if it works.
http://cseweb.ucsd.edu/~dasgupta/103/5a.pdf
http://cseweb.ucsd.edu/~dasgupta/103/5b.pdf
Last edited: