Saturday, December 5, 2009

C++ Random Numbers - C++

a short note on the generation of random numbers. There are several things to notice
1. use srand(time(0)) as a seed for random number generator. Note that we only need to call srand just once even if we generate a lot of rand numbers.
2. we can use rand( ) % (r - l + 1) + l to generate numbers between [l,u] with equal probability. However, there is another method of using l + (rand( ) * (r - l + 1))/(RAND_MAX + 1.0) to get the numbers. I did not see any explanations why the later one is better.

C++ Random Numbers

This post is useful and well-written 0 This post is unclear
#1
Nov 16th, 2003
Intro

This tutorial provides a brief introduction to the random number functions that come as part of the C++ standard library, namely rand() and srand().

rand() and RAND_MAX

The C++ standard library includes a pseudo random number generator for generating random numbers. In order to use it we need to include the header. To generate a random number we use the rand() function. This will produce a result in the range 0 to RAND_MAX, where RAND_MAX is a constant defined by the implementation.

Here's a piece of code that will generate a single random number:

Help with Code Tags
C++ Syntax (Toggle Plain Text)

1.
#include
2.
#include
3.

4.
using namespace std;
5.

6.
int main()
7.
{
8.
int random_integer = rand();
9.
cout << random_integer << endl;
10.
}

#include #include using namespace std; int main() { int random_integer = rand(); cout << random_integer << endl; }
The value of RAND_MAX varies between compilers and can be as low as 32767, which would give a range from 0 to 32767 for rand(). To find out the value of RAND_MAX for your compiler run the following small piece of code:

Help with Code Tags
C++ Syntax (Toggle Plain Text)

1.
#include
2.
#include
3.

4.
using namespace std;
5.

6.
int main()
7.
{
8.
cout << "The value of RAND_MAX is " << RAND_MAX << endl;
9.
}

#include #include using namespace std; int main() { cout << "The value of RAND_MAX is " << RAND_MAX << endl; }
srand()

The pseudo random number generator produces a sequence of numbers that gives the appearance of being random, when in fact the sequence will eventually repeat and is predictable.

We can seed the generator with the srand() function. This will start the generator from a point in the sequence that is dependent on the value we pass as an argument. If we seed the generator once with a variable value, for instance the system time, before our first call of rand() we can generate numbers that are random enough for simple use (though not for serious statistical purposes).

In our earlier example the program would have generated the same number each time we ran it because the generator would have been seeded with the same default value each time. The following code will seed the generator with the system time then output a single random number, which should be different each time we run the program.

Help with Code Tags
C++ Syntax (Toggle Plain Text)

1.
#include
2.
#include
3.
#include
4.

5.
using namespace std;
6.

7.
int main()
8.
{
9.
srand((unsigned)time(0));
10.
int random_integer = rand();
11.
cout << random_integer << endl;
12.
}

#include #include #include using namespace std; int main() { srand((unsigned)time(0)); int random_integer = rand(); cout << random_integer << endl; }
Don't make the mistake of calling srand() every time you generate a random number; we only usually need to call srand() once, prior to the first call to rand().

Generating a number in a specific range

If we want to produce numbers in a specific range, rather than between 0 and RAND_MAX, we can use the modulo operator. It's not the best way to generate a range but it's the simplest. If we use rand()%n we generate a number from 0 to n-1. By adding an offset to the result we can produce a range that is not zero based. The following code will produce 20 random numbers from 1 to 10:

Help with Code Tags
C++ Syntax (Toggle Plain Text)

1.
#include
2.
#include
3.
#include
4.

5.
using namespace std;
6.

7.
int main()
8.
{
9.
srand((unsigned)time(0));
10.
int random_integer;
11.
for(int index=0; index<20; index++){
12.
random_integer = (rand()%10)+1;
13.
cout << random_integer << endl;
14.
}
15.
}

#include #include #include using namespace std; int main() { srand((unsigned)time(0)); int random_integer; for(int index=0; index<20; index++){ random_integer = (rand()%10)+1; cout << random_integer << endl; } }
A better method, though slightly more complicated, is given below. This overcomes problems that are sometimes experienced with some types of pseudo random number generator that might be supplied with your compiler. As before, this will output 20 random numbers from 1 to 10.

Help with Code Tags
C++ Syntax (Toggle Plain Text)

1.
#include
2.
#include
3.
#include
4.

5.
using namespace std;
6.

7.
int main()
8.
{
9.
srand((unsigned)time(0));
10.
int random_integer;
11.
int lowest=1, highest=10;
12.
int range=(highest-lowest)+1;
13.
for(int index=0; index<20; index++){
14.
random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
15.
cout << random_integer << endl;
16.
}
17.
}

#include #include #include using namespace std; int main() { srand((unsigned)time(0)); int random_integer; int lowest=1, highest=10; int range=(highest-lowest)+1; for(int index=0; index<20; index++){ random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); cout << random_integer << endl; } }

Conclusion

If you need to use a pseudo random number generator for anything even remotely serious you should avoid the simple generator that comes with your compiler and use something more sophisticated instead. That said, rand() still has its place and you may find it useful.

No comments:

Post a Comment