On Tue, Jan 28, 2003 at 12:57:04AM +0800, Geoffrey wrote:
>
> If I mod it with 5, then the result will be 0, 1, 2, 3, 4, 0, 1, 2, 3,
> 4, 0, 1 (each have same probability to gen), then the chance of the "0"
> and "1" return is 3/12 and the other is 2/12. The probaility will not be
> same for each value!
>
> I am now try to give a random result of a dice, it is not fair that some
> number have more probaility to return!
I will cite section 3 of rand function man page (glibc):
In Numerical Recipes in C: The Art of Scientific Computing
(William H. Press, Brian P. Flannery, Saul A. Teukolsky,
William T. Vetterling; New York: Cambridge University
Press, 1992 (2nd ed, p. 277)), the following comments are
made:
"If you want to generate a random integer between 1
and 10, you should always do it by using high-order
bits, as in
j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
and never by anything resembling
j=1+(rand() % 10);
(which uses lower-order bits)."
Random-number generation is a complex topic. The Numeri�
cal Recipes in C book (see reference above) provides an
excellent discussion of practical random-number generation
issues in Chapter 7 (Random Numbers).
For a more theoretical discussion which also covers many
practical issues in depth, please see Chapter 3 (Random
Numbers) in Donald E. Knuth's The Art of Computer Program�
ming, volume 2 (Seminumerical Algorithms), 2nd ed.; Read�
ing, Massachusetts: Addison-Wesley Publishing Company,
1981.
Hope this helps.
Ciao,
Marco
--
========================================================================
Marco Pantaleoni [EMAIL PROTECTED]
Padova, Italy [EMAIL PROTECTED]
elastiC language developer http://www.elasticworld.org
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/support/forums/