Random number generation is a very useful facility in many programming situations, particularly gaming and simulation. We can also use it to generate test data for programs, particularly when a large amount of data is required. This saves a lot of time on data entry.
A random number is one whose value cannot be predicted. It is nearly impossible to get truly random numbers. However, one may use pseudorandom numbers which are generated. The C standard library provides a function named rand to generate pseudo-random numbers. This function is declared in the stdlib.h header file. It does not require any argument and returns an integer number in the range 0 to RAND_MAX, where RAND_MAX is a constant defined in stdlib.h with value 32767.
We can manipulate the value returned by the the rand function to generate integer or floating-point numbers in a desired range. The expression
(float) rand() / RAND_MAX
gives a random number of type float in the range [0.0, 1.0], i. e., 0 to 1.0, both inclusive. Note the use of typecast (float) to avoid integer division. To generate a random number in a desired range m ton, we can now use the following expression:
m + (m - n) * (float) rand() /RAND_MAX
The rand function actually generates pseudo-random data. Thus, each time the program is executed, it generates the same sequence of random numbers. This may not be acceptable in many situations. Hence, another function called srand is provided to reseed the random number generator. A typical call to this function takes the following form:
srand (seed)
where seed is an unsigned integer. This call reseeds the random number generator so that a different sequence of random numbers is generated on subsequent calls to rand.
Random numbers generation with rand() with default seed.
#include<stdio.h>
#include<stdlib.h>
int main ()
{
int n ;
clrscr();
for (n=1; n< 7; n++)
printf("%d\t",rand());
return 0;
}
The output of first trial is as below
The result of second trial is same as that of first as given below
Note that the two sets of random numbers are identical. In fact every time the function rand ()is used without changing the seed number, it will produce same set of random numbers. In order to get a different single or set of random numbers a different seed number has to be provided through function srand ().Program provides an illustration of generating one random number with the same seed numbers.
Random number generation with same seed number
#include<stdio.h>
#include<stdlib.h>
int main()
{
unsigned seed;
int n ;
clrscr();
seed = 1;
for (n=1;n< 4;n++)
{
srand(seed);
printf("%d\t",rand()) ;
}
printf ("\n");
return 0;
}
The expected output is as given below
The output illustrates that if the seed number is 1 then the random number generated is 346. This is also called default seed number; when a user does not provide a seed number, the system provides 1 as the seed number.
When a set of random numbers is generated, for the first number, the system provides 1 as the seed number and the random number generated is used as the seed for the next random number. Program computes rand () with a seed number provided by the user of the program. Therefore, for every application of rand () a different seed number is required to produce a different random number. That is, while producing a set of random numbers, if the first random number is different, then the whole set would be different.
Illustrates random number generation with a seed number
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n , S;
clrscr();
printf("Enter an integer seed number : ");
scanf("%d", &S);
srand(S);
for (n=1;n<=6;n++)
printf("%d\t", rand());
return 0;
}
The output is as given below.
Dinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.
Related Articles
Basic Courses
Advance Courses