Dev C++ Random Number Generator
Random number generators fulfill a number of purposes. Everything from games to simulations require a random number generator to work properly. Randomness finds its way into business what-if scenarios as well. In short, you need to add random output to your application in many situations.
The srand function in C seeds the pseudo random number generator used by the rand function. The seed for rand function is 1 by default. It means that if no srand is called before rand, the rand function behaves as if it was seeded with srand(1).
Creating a random number isn’t hard. All you need to do is call a random number function as shown in the RandomNumberGenerator example:
Actually, not one of the random number generators in the Standard Library works properly — imagine that! They are all pseudorandom number generators: The numbers are distributed such that it appears that you see a random sequence, but given enough time and patience, eventually the sequence repeats.
In fact, if you don’t set a seed value for your random number generator, you can obtain predictable sequences of numbers every time. How boring. Here is typical output from this example:
The first line of code in main() sets the seed by using the system time. Using the system time ensures a certain level of randomness in the starting value — and therefore a level of randomness for your application as a whole. If you comment out this line of code, you see the same output every time you run the application.
The example application uses rand() to create the random value. When you take the modulus of the random number, you obtain an output that is within a specific range — 12 in this case. The example ends by adding 1 to the random number because there isn’t any month 0 in the calendar, and then outputs the month number for you.
The Standard Library provides access to two types of pseudorandom number generators. The first type requires that you set a seed value. The second type requires that you provide an input value with each call and doesn’t require a seed value. Each generator outputs a different data type, so you can choose the kind of random number you obtain.
The table lists the random number generators and tells you what data type they output.
Function | Output Type | Seed Required? |
---|---|---|
rand | integer | yes |
drand48 | double | yes |
erand48 | double | no |
lrand48 | long | yes |
nrand48 | long | no |
mrand48 | signed long | yes |
jrand48 | signed long | no |
Now that you know about the pseudorandom number generators, look at the seed functions used to prime them. The following table lists the seed functions and their associated pseudorandom number generator functions.
Function | Associated Pseudorandom Number Generator Function |
---|---|
srand | rand |
srand48 | drand48 |
seed48 | mrand48 |
lcong48 | lrand48 |
C program to generate pseudo-random numbers using rand and random function (Turbo C compiler only). As the random numbers are generated by an algorithm used in a function they are pseudo-random, this is the reason that word pseudo is used. Function rand() returns a pseudo-random number between 0 and RAND_MAX. RAND_MAX is a constant which is platform dependent and equals the maximum value returned by rand function.
C programming code using rand
We use modulus operator in our program. If you evaluate a % b where a and b are integers then result will always be less than b for any set of values of a and b. For example
For a = 1243 and b = 100
a % b = 1243 % 100 = 43
For a = 99 and b = 100
a % b = 99 % 100 = 99
For a = 1000 and b = 100
a % b = 1000 % 100 = 0
In our program we print pseudo random numbers in range [0, 100]. So we calculate rand() % 100 which will return a number in [0, 99] so we add 1 to get the desired range.
#include <stdio.h>#include <stdlib.h>
int main(){
int c, n;
printf('Ten random numbers in [1,100]n');
for(c =1; c <=10; c++){
n =rand()%100+1;
printf('%dn', n);
}
return0;
}
If you rerun this program, you will get the same set of numbers. To get different numbers every time you can use: srand(unsigned int seed) function; here seed is an unsigned integer. So you will need a different value of seed every time you run the program for that you can use current time which will always be different so you will get a different set of numbers. By default, seed = 1 if you do not use srand function.
C programming code using random function (Turbo C compiler only)
C++ Random Number Generator Code
Function randomize is used to initialize random number generator. If you don't use it, then you will get same random numbers each time you run the program.
#include <stdio.h>#include <conio.h>
#include <stdlib.h>
int main()
{
int n, max, num, c;
printf('Enter the number of random numbers you wantn');
scanf('%d',&n);
C++ Random Number Generator Function
printf('Enter the maximum value of random numbern');
scanf('%d',&max);
printf('%d random numbers from 0 to %d are:n', n, max);
randomize();
for(c =1; c <= n; c++)
{
num = random(max);
printf('%dn',num);
}
getch();
return0;
}