Random Function in C
In this topic, we will learn about the random function and how we can generate the random number in the C programming language. As we know, the random function is used to find the random number between any two defined numbers. In the C programming language, the random function has two inbuilt functions: rand() and srand() function. Let’s understand these functions in the C language.
rand() function
In the C programming language, the rand() function is a library function that generates the random number in the range [0, RAND_MAX]. When we use the rand() function in a program, we need to implement the stdlib.h header file because rand() function is defined in the stdlib header file. It does not contain any seed number. Therefore, when we execute the same program again and again, it returns the same values.
Note: If the random numbers are generated with the rand() function without calling the srand() function, it returns the same sequences of numbers each time the program is executed.
Syntax
The rand() function returns the random integers whose range from 0 to RAND_MAX. The RAND_MAX is a symbolic constant that defines in stdlib.h header file, whose value is greater but less than 32767 depending on the C libraries.
Generate the random numbers using the rand() function
Let’s write a program to get the random number using rand() function.
num.c
Output
The random number is: 41 The random number is: 18467 The random number is: 6334 The random number is: 26500
Generate 5 random numbers using rand() function
Let’s consider a program to generate 5 random number using rand() function in C programming language.
random.c
Output
Random Numbers are: 41 18467 6334 26500 19169
2nd execution of the program:
Random Numbers are: 41 18467 6334 26500 19169
3rd execution of the program
Random Numbers are: 41 18467 6334 26500 19169
As we can see in the above output, it returns the same sequence of random numbers on every execution of the programming code.
Generate 10 random numbers from 1 to 100 using rand() function
Let’s consider a program to find the random number in C using rand() function.
rand_num.c
Output
Program to get the random number from 1 to 100 42 68 35 1 70 25 79 59 63 65
srand() function
The srand() function is a C library function that determines the initial point to generate different series of pseudo-random numbers. A srand() function cannot be used without using a rand() function. The srand() function is required to set the value of the seed only once in a program to generate the different results of random integers before calling the rand() function.
Syntax
seed: It is an integer value that contains a seed for a new sequence of pseudo-random numbers.
Generate the random numbers using srand() function
Let’s write a program to get the random numbers using srand() function in C.
srandNum.c
Output
Enter a number to set the limit for a random number 10 44 32 23 35 6 33 1 4 22 18
2nd execution of the program:
Enter a number to set the limit for a random number 15 13 30 24 27 4 30 28 35 36 13 44 39 21 5 7
As we can see in the above Output, it returns different sequences of random numbers on every execution of the programming code.
Generate the random numbers using srand() and time() function
Let’s write a program to get the random numbers using srand() with time() function.
srand_time.c
Output
Seed = 1619450091 Random number = 41
Get a seeding value and print the random numbers using srand() function
Let’s write a program to get the seed value and random numbers using srand() function.
srand_time.c
Output
Enter the Seeding value: 10 6 4 3 3 6 3 3 1 3 4
2nd execution of the program:
Enter the Seeding value: 20 2 4 2 4 5 4 3 5 1 4
3rd execution of the program:
Enter the Seeding value: 25 1 6 1 6 4 4 1 4 1 3
As we can see in the above Output, when we executed the same program again and again with different seeds values, it displays the different sequences of a random number from 1 to 6.
Generate the random number using the random function
Let’s create a program to use stadlib header file to get the random number using random function in C.
func.c
Output
Enter a number to set the limit of random numbers 17 Enter the maximum number from you want to get the random number: 100 15 random number from 0 to 100 number are: 42 68 35 1 70 25 79 59 63 28 75 89 90 43 7 4 65
Program to generate float random numbers
Let’s consider a program to print the float random numbers in C.
random1.c
Output
Float random numbers are: 1.208075 1.658376 4.645070 2.298807 3.117161 0.961486 4.115573 4.336223 2.894833 2.249825