Sponsored Content
Full Discussion: mod on %RANDOM
Top Forums Shell Programming and Scripting mod on %RANDOM Post 302272025 by Perderabo on Monday 29th of December 2008 10:29:11 AM
Old 12-29-2008
When you use a modulus operation you are selecting information from the low order bits of a number and discarding information from the high order bits.

"these bits should be extracted from the most significant (left-hand) part
of the computer word, since the least significant bits produced by many random number generators are not sufficiently random."

and

"The least significant (right-hand) digits of X are not very random, so decisions based on the number X should always be influenced primarily by the most significant digits. It is generally best to think of X as a random fraction X/m between 0 and 1, that is, to visualize X with a decimal point at its left, rather than to regard X as a random integer between 0 and m - 1. To compute a random integer between 0 and k - 1, one should multiply by k and truncate the result."
both from The Art of Computer Programming, Volume 2 Seminumerical Algorithms by Donald Knuth

While neither is perfect, assuming that 0 <= RANDOM <= 32767,
((myrandom = RANDOM * 50 / 32768))
will behave better than
((myrandom = RANDOM % 50))
 

3 More Discussions You Might Find Interesting

1. Programming

Mod

I try to use mod(a,b) in a program but i cant Please help me (2 Replies)
Discussion started by: iwbasts
2 Replies

2. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

3. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies
RAND(3) 						     Linux Programmer's Manual							   RAND(3)

NAME
rand, srand - random number generator. SYNOPSIS
#include <stdlib.h> int rand(void); void srand(unsigned int seed); DESCRIPTION
The rand() function returns a pseudo-random integer between 0 and RAND_MAX. The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value. If no seed value is provided, the rand() function is automatically seeded with a value of 1. RETURN VALUE
The rand() function returns a value between 0 and RAND_MAX. The srand() returns no value. NOTES
The versions of rand() and srand() in the Linux C Library use the same random number generator as random() and srandom(), so the lower- order bits should be as random as the higher-order bits. However, on older rand() implementations, the lower-order bits are much less ran- dom than the higher-order bits. 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 Numerical 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 Programming, volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts: Addison-Wesley Publishing Company, 1981. CONFORMING TO
SVID 3, BSD 4.3, ISO 9899 SEE ALSO
random(3), srandom(3), initstate(3), setstate(3) GNU
1995-05-18 RAND(3)
All times are GMT -4. The time now is 10:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy