I have written the following code to generate random numbers in C.
int main (int argc, char *argv[])
{
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);
/* Generate random number*/
int i;
for (i = 0; i < 1; i++)
{
printf ("Random[%d]= %u\n", i, rand ());
}
return 0;
}
The output gives me a 10 digit random number, how can I change the number of digits printed in the output?
rand()gives you a number between 0 andRAND_MAX, which may be a large number.If you want to get a uniform sample in the range
[0, N), you need to divide the range into regions:The condition on line
#1should be met only very rarely, but we need to re-roll in the case where the result doesn’t lie inside a range that is a multiple ofNto avoid bias against high results.