Mail Archives: djgpp/1996/11/30/06:43:48
>Anyway, here's an example of the correct way to use
>random()...
>
> ... header stuff cut out .....
>
> while ( tolower( getchar( ) ) != 'q' )
> {
> r = random( ) % 501; /* 0 to 500 */
> printf( "%d", r );
> }
I hate to say this, but it is NOT generally advisable to use MODULUS to
force a random number into a specified range if you really care about the
quality of the random numbers. Depending on the exact modulus used (N = 501
above) you may get a 'random' sequence that repeats very often (in worst
case once every N shots!) - essentially the random number generator is being
truncated to a log2(N) bit generator instead of 32 bit
You really need to bite the bullet and do a divide ...
------------------------------------------------------------------------
Paul Dixon
Software Engineer, Dept of Paediatrics Telephone: (+44) 171 725 6258
St Mary's Hospital Medical School Fax: (+44) 171 725 6284
Norfolk Place, London W2 1PG, UK Email: p DOT dixon AT ic DOT ac DOT uk
A Constituent College of
Imperial College of Science, Technology and Medicine
------------------------------------------------------------------------
- Raw text -