## Random Numbers Randomized

Random Numbers Randomized (C++ Implementation)

Sometime or the other you need to generate random numbers for your programs as they play a very important role in computer applications, especially in simulations. A particularly useful random number sequence is the "uniform random number sequence". It has a specified set of numbers from which the sequence draws its random numbers. In each position of the random number sequence, any no. from the set is equally likely to occur.
Because a random number sequence is supposed to be random, there can't be any computer algorithm that iteratively computes truly random numbers. The instructions that constitute an algorithm are deterministic rules-knowing them tells you the next number. However, some functions do produce sequences of numbers that appear to be random. These sequences are called "Pseudorandom number sequences", although most people are imprecise and drop the prefix pseudo.
The C++ stdlib library provides 2 functions that are useful in generating pseudorandom number sequences. They are rand() and srand() and they are declared in stdlib.h. Function rand() takes no parameters. Each time it is invoked, it returns a uniform pseudorandom number from the inclusive interval 0 to RAND_MAX, where RAND_MAX is an implementation dependent preprocessor macro constant defined in stdlib.h. In most implementations, the generation of the current pseudorandom number is a function of the previously generated pseudorandom number. The generation of the first pseudorandom number by a program is based on a similar function of an initial value, called the seed, that is supplied to the pseudorandom number generator.
The program given below generates 5 pseudorandom numbers.
-------Code Begin------------
#include <iostream.h>
#include <stdlib.h>
int main()
{
for(int i=1;i<=5;++i)
{
cout<<rand()<<endl;
}
return 0;
}
-------Code End----------------
But when you run this program, you will see that every time the program is run, the same set of 5 numbers is generated. Why is this??. Well, this repetition is part of the design of the function so that while the program is being tested or examined, it is possible to reproduce the same statement execution sequence.
However, if you want to produce a different sequence of pseudorandom numbers, the function srand() is used. Function srand() expects an unsigned int as its parameter which is used to set the seed for generating the first pseudorandom number. Once the seed is set, rand(), should produce a different sequence of random numbers. In the program given below, the user provides the seed value that is to be passed to srand().
-------Code Begin------------
#include <iostream.h>
#include <stdlib.h>
int main()
{
cout<<"Random number seed (number): ";
unsigned int seed;
cin>>seed;
srand(seed);
for(int i=1;i<=5;++i)
{
cout<<rand()<<endl;
}
return 0;
}
-------Code End----------------
Now, here if the user has to undergo the hassles of providing the seed every time and if we write a constant no. as the seed in the source code itself, it will again generate the same sequence every time. So, we use the current time as the basis for the seed value because that way, the seed should be different for each run of the program. The current time is determined using the function time(), which is defined in the time standard library. Function time() returns a value of type time_t, which is an integral type and has to be type cast into unsigned int before it can be passed to srand(). An implementation for such a program is given below.
-------Code Begin------------
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand((unsigned int) time(0));
for(int i=1;i<=5;++i)
{
cout<<rand()<<endl;
}
return 0;
}
-------Code End----------------
Now, you will see that every time the program is run, a new sequence is generated. But many times, we need to generate integral numbers between two integral values. Like I recently made a Cricket match simulator and I needed to generate a number between 1 and 6 for the runs.
For such situations, we develop a function that allows us to pass the lower value and higher value between which the random number is to be generated (inclusively, i.e., including both the higher value as well as the lower value)
-------Code Begin------------
int Random(int Low, int High)
{
int IntervalSize=High-Low+1;
int RandomOffset=rand()%IntervalSize;
return Low+RandomOffset;
}
-------Code End----------------
Now, you can use this fuction in any of your programs and call it whenever needed it. But, remember to call srand((unsigned int) time(0)); before calling this function. Also, you may want to palce a simple check in the above function to check whether Low is less than High or not.

So, that's it for now. Hope I have been able to make you understand properly this simple but sometimes overlooked concept.

Let me know if i missed something or if i was worng somewhere.