Two common methods for random number generation are as below:
One
is from the manpage of “srand”:
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 random 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 follow-ing 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 prac-tical 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.
==============================
A
small example: Generate 100 random number between 0-9
#include
<iostream>
#include <ctime>
using namespace
std;
int main()
{
//srand(0); //set 0 as random
seed
srand(time(0)); //use current time as seed
for (unsigned
i=0;i<100;++i) {
cout << int(10.0*(rand()/(RAND_MAX +
1.0)));
}
cout << endl;
return 0;
}
The
other method is “Linear congruential generators” (from
http://en.wikipedia.org/wiki/Linear_congruential_generator)
Linear
congruential generators (LCGs) represent one of the oldest and
best-known pseudo random number generator algorithms. The theory
behind them is easy to understand, and they are easily implemented
and fast. It is, however, well known that the properties of this
class of generator are far from ideal. If higher quality random
numbers are needed, and sufficient memory is available (~ 2 KBytes),
then the Mersenne twister algorithm is a preferred choice.
LCGs
are defined by the recurrence relation:
Vj+1 = ( A
* Vj + B) mod M
Where Vn is the sequence of random
values and A, B and M are generator-specific integer constants. mod
is the modulo operation. The period of a general LCG is at most M,
and in most cases less than that. The LCG will have a full period
if:
1. B and M are relatively prime
2. A-1 is divisible by
all prime factors of M.
3. A-1 is a multiple of 4 if M is a
multiple of 4
4. M > max(A, B, V0)
5. A > 0, B > 0
Most
often M = 232 or M = 264, could make a good efficiency to
performance tradeoff. These are the fastest-evaluated of all random
number generators; a common Mersenne twister implementation uses it
to generate seed data. Numerical Recipes in C advocates a generator
of this form with:
A = 1664525, B = 1013904223, M = 232
==============================
A
small example: Generate 100 random number between 0-9
#include
<iostream>
#include <ctime>
using namespace
std;
int main()
{
const unsigned A(1664525), B(1013904223),
M(232);
srand(time(0));
unsigned num = rand() % M; //take
first random number
for (unsigned i=0;i<100;++i) {
num = (A
* num + B) % M;
cout << int(10.0*num/double(M+1));
}
cout
<< endl;
return(0);
}