If we are generating random numbers in a program, it is necessary to control the series of numbers.
The randomize() and srand() functions are used to seed the random number generator.
The process of assigning the random number generators starting number is called seeding the generators.
The randomize() uses PC’s clock to produce a random seed.
srand() allows us to specify the random number generator’s starting value.
Program
Given below is the C program on rand −
#include<stdio.h> int main(){ // create same sequence of // random numbers on every time the program runs for(int i = 0; i<10; i++) printf(" %d ", rand()); return 0; }
Output
You will see the following output −
1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386 1649760492 596516649 1189641421
Following is the C program on srand −
#include <stdio.h> #include <stdlib.h> #include<time.h> int main(){ // create different sequence of // random numbers on every time the program runs // It Use current time as seed for random generator srand(time(0)); for(int i = 0; i<10; i++) printf(" %d ", rand()); return 0; }
Output
You will see the following output −
1919778910 1203408690 1755813469 1976428341 37040990 1849384103 986990763 2040061815 391541163 1718314135