Header File:
stdlib.h
Syntax:
int rand (void)
Return values:
This function returns the next pseudo-random number in the series. The range value of the number series is between 0 and RAND_MAX. RAND_MAX is a macro defined in stdlib.h header file, whose value is the maximum value, which can return by rand() function. The value of RAND_MAX is greater but not less than 32767 depending on the C libraries.
In Example1.c, we call the rand() function in each iteration of for loop and print the return value of the function. The value sequence of the rand() function is the same each time we run the program. By default, the seed of the rand function is set to 1.
We can set the seed for the rand function using the srand() function. The seed can be set only once, and before the first time rand() function call.
srand() function:
Header File:
stdlib.h
Syntax:
int srand (unsigned int seed)
Arguments:
This function takes 1 argument
seed: An integer value used as a seed for a new series of pseudo-random numbers.
Return values:
None
In Example2.c, we have used the srand() function to set the initial seed of the random number sequence generated by rand() function. Each time the program is run, a different sequence is generated. In srand(), time(0) function (declared in time.h header file) is used as a seed. This time(0) function returns the number of seconds elapsed since the epoch (00:00:00, January 1, 1970). This still may produce the same sequences if you run the program in the same second.
In Example3.c we have seen how random numbers can be generated between 1 and 10.
#include
#include
#include
int main()
{
int i,max,min;
printf("Enter Min value => ");
scanf("%d",&min);
printf("Enter Max value => ");
scanf("%d",&max);
if(min>max)
{
printf("Min value is greater than max value\n");
return 0;
}
srand(time(0));
printf("10 Random Numbers between %d and %d=>\n",min,max);
for(i=0;i<10;i++)
{
printf("%d ",(rand() % (max - min +1)) + min);
}
printf("\n");
return 0;
}
In Example4.c we have taken the range from the user and generated a random number within this range. The formula is: rand() % (max – min +1)) + min
In Example5.c, we have seen how we can generate random numbers between float 0.0 and 1.0 The formula is: (float)rand() /RAND_MAX)
#include
#include
#include
int main()
{
int i;
float max,min;
printf("Enter Min value => ");
scanf("%f",&min);
printf("Enter Max value => ");
scanf("%f",&max);
if(min>max)
{
printf("Min value is greater than max value\n");
return 0;
}
srand(time(0));
printf("10 Random Numbers between %f and %f =>\n",min,max);
for(i=0;i<10;i++)
{
printf("%f ",min + ((float)rand() /(RAND_MAX/(max - min))));
}
printf("\n");
return 0;
}
In Example6.c, we have taken the range from the user and generated a random number within this range (both inclusive). The formula is: min + ((float)rand() /(RAND_MAX/(max – min)))
Conclusion:
In this article, we have learned how random numbers can be generated using the rand() and srand() function. There are no guarantees about the quality of the random numbers generated by the rand function, but it is good enough for casual use.
Sources:
n.d., (2008, October 14). Purpose of the GNU C Library.. Retrieved from http://web.archive.org/web/20081014160228/http://waitaki.otago.ac.nz/docs/glibc/libc_19.html
Matsumoto, Y. (2001, November). Ruby in a Nutshell. Retrieved from https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.170.3456&rep=rep1&type=pdf
from Linux Hint https://ift.tt/35scjN9
0 Comments