C Program to generate random numbers

33

To generate the random numbers, C provides the predefined functions called rand() and srand() in library stdlib.h. The rand() function basically returns the random number and srand() function sets the starting position for the rand() function, in this article we will going to see the how rand() and srand() function works with an example.

rand() Function:

The rand() function returns the random number but it gives you the same sequence no matter how many times you compile and run the code, it gives the same sequence as previous.

Whenever we compile and run the code our output must be the same sequence of numbers in rand() function.

Syntax of rand() function:

It returns the random number in the range of 0 to RAND_MAX.

Where RAND_MAX is the constant whose default value may vary during implementations, it is granted to be at least 32767.

Consider the following example for better understanding about rand() function:

Example of rand() function:

// C program to generate random numbers

#include <stdio.h>

#include <stdlib.h>

// Main Logic

int main (void)

{

    // This main functions will print the same sequence of the random numbers on every program run

    for (int i = 0; i<=4; i++)

        printf (” %d”, rand());

    return 0;

}

After the first time Compilation and Executes the program the Output is:

The second time Compilation and Executes the program the Output is:

The third time Compilation and Executes the program the Output is:

srand() Function:

The srand() function sets the starting position of the rand() function for the series of random integers. If srand() is not defined then compiler considered as srand(1) by default.

Syntax of srand() function:

void srand (unsigned value)

The unsigned values in srand() function is the random number generator used by the rand() function.

Now our task is to generate the random number each and every time when we compile and run the program.

Example of srand() function:

// C program to generate random numbers

#include <stdio.h>

#include <stdlib.h>

#include<time.h>

// Main Logic

int main(void)

{

               //This main functions will print the same sequence of the random numbers on every program run

               // Use current time as unsign value for the random generator

               srand(time(0));

               for(int i = 0; i<=4; i++)

                              printf(” %d “, rand());

               return 0;

}

After the first time Compilation and Executes the program the Output is:

25903 18245 12020 20354 8886

The second time Compilation and Executes the program the Output is:

25952 15631 17839 20856 32532

The third time Compilation and Executes the program the Output is:

26004 23766 8753 12653 958

C Program to Generate Random Numbers

Here is the program to print the random number in series with given count N and all numbers in between the start and end point.

Program:

// C program for generating the random number in the given range.

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

// Generates and prints ‘count’ i.e n random

// numbers in range [start, end].

void printRandomNum(int start, int end, int n)

{

               int i;

               for (i = 0; i < n; i++) {

                              int num = (rand() % (end – start + 1)) + start;

                              printf(“%d “, num);

               }

}

// Main Function

int main()

{

               int start, end, n;

               printf(“Start point: “);

               scanf(“%d”, &start);

               printf(“End point: “);

               scanf(“%d”, &end);

               printf(“Count/N: “);

               scanf(“%d”, &n);

               // Use current time as

               // seed for random generator

               srand(time(0));

               printRandomNum(start, end, n);

               return 0;

}

Output:

Start point: 8

End point: 20

Count/N: 5

19 12 18 14 19

LEAVE A REPLY

Please enter your comment!
Please enter your name here