rand function
In C programming, the rand function is a built-in function that produces a random number within the specified range of [0, RAND_MAX]. To incorporate the rand function into a program, it is essential to include the stdlib.h header file as the function is declared within it. Notably, the rand function does not rely on a seed number. Consequently, running the program repeatedly will yield identical results each time.
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
int rand (void)
The rand function generates random integers within the range of 0 to RANDMAX. RANDMAX is a symbolic constant specified in the stdlib.h header file, with a value that varies based on the C libraries but is always greater than 32767.
Generate the random numbers using the rand function
Let's create a script to generate a random number using the rand function.
num.c
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
// use rand() function to generate the number
printf (" The random number is: %d", rand());
printf ("\n The random number is: %d", rand());
printf (" \n The random number is: %d", rand());
printf ("\n The random number is: %d", rand());
getch();
}
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 explore a program that generates five random numbers using the rand function in the C programming language.
random.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
/* It returns the same sequence of random number
on every execution of the program. */
printf(" Random Numbers are: \n");
for (i = 0; i <5; i++)
{
printf(" %d", rand());
}
return 0;
}
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
In the preceding output, it is evident that the programming code consistently generates an identical sequence of random numbers with each execution.
Generate 10 random numbers from 1 to 100 using rand function
Let's explore a program that generates a random number in C by utilizing the rand function.
rand_num.c
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
// declare the local variables
int i, num;
printf (" Program to get the random number from 1 to 100 \n");
for (i = 1; i <= 10; i++)
{
num = rand() % 100 + 1; // use rand() function to get the random number
printf (" %d ", num);
getch();
}
}
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 within the C library is responsible for establishing the starting point for producing various sequences of pseudo-random numbers. It is essential to note that utilizing the srand function necessitates the prior use of the rand function. This function is crucial for initializing the seed value singularly within a program to ensure the generation of distinct sets of random integers each time before invoking the rand function.
Syntax
int srand (unsigned int seed)
seed : An integer value that holds a seed for generating a fresh sequence of pseudo-random numbers.
Generate the random numbers using srand function
Let's create a program that generates random numbers using the srand function in the C programming language.
srandNum.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h> // use time.h header file to use time
int main()
{
int num, i;
time_t t1; // declare time variable
printf(" Enter a number to set the limit for a random number \n");
scanf (" %d", &num);
/* define the random number generator */
srand ( (unsigned) time (&t1)); // pass the srand() parameter
printf("\n"); // print the space
/* generate random number between 0 to 50 */
for (i = 0; i <num; i++)
{
printf( "%d \n", rand() % 50);
}
return 0;
}
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 depicted in the prior Output, it produces varying sequences of random numbers each time the code is executed.
Generate the random numbers using srand and time function
Let's create a program that generates random numbers by using the srand function along with the time function.
srand_time.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int random = rand(); // assign the rand() function to random variable
srand( time(0));
printf( " Seed = %d", time(0));
printf( " Random number = %d", random);
return 0;
}
Output
Seed = 1619450091 Random number = 41
Get a seeding value and print the random numbers using srand function
Let's develop a program to retrieve the initial seed value and generate random numbers utilizing the srand function.
srand_time.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int count;
unsigned int seed; // use for randomize number
printf(" Enter the Seeding value: \n");
scanf(" %u", &seed);
srand (seed); // pass parameter
// generate random number between 1 to 6
for (count = 1; count <= 10; ++count)
{
printf(" %5d", 1 + (rand () % 6));
if (count % 5 == 0) // print the number in next line
puts(" ");
}
return 0;
}
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
Upon reviewing the previous Output, it becomes evident that upon successive executions of the program with varying seed values, distinct sequences of random numbers ranging from 1 to 6 are generated.
Generate the random number using the random function
Let's develop a program that utilizes the stadlib header file to generate a random number by employing the random function in the C programming language.
func.c
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
int i, num, max, temp;
printf (" Enter a number to set the limit of random numbers \n");
scanf ("%d", num);
printf (" Enter the maximum number from you want to get the random number: \n");
scanf ("%d", max);
printf (" %d random number from 0 to %d number are: \n", num, max);
randomize();
for (i = 1; i <= num; i++)
{
temp = random(max) /* use random() function to get the random number */
printf (" %d ", temp); // print the temp
}
getch();
}
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 explore a program designed to display floating-point random numbers in the C programming language.
random1.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand( (unsigned int) time(NULL));
float f1 = 5.0;
int i;
printf(?Float random numbers are: \n?);
for (i = 0; i<10; i++)
{
printf("%f", ((float) rand()/ RAND_MAX) * f1);
printf("\n");
}
return 0;
}
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