Many algorithm correctness and performance tests, games and others require random number generation. Here is a way to generate a random number between two limits min and max.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int randomnum;
int min=1; /* lower limit */
int max=7; /*upper limit */
time_t seconds;
time(&seconds);
srand((unsigned int) seconds); /*seed for rand() */
randomnum = rand() % (max-min+1) + min; /*generate random number within limits*/
printf(“random number=%d\n”,randomnum);
return 0;
}
The idea behind the solution is very simple.
- rand() generates a random integer between 0 and RAND_MAX. The number generated depends on the seed
- rand()%max+1 generates an integer between 1 and max
- On the same lines, rand()%(max-min+1) generates a random number between 0 and (max-min) thereby randomizing the difference between the two limits. To this random difference, we can add a lower limit min to generate the random integer between min and max
Nice.
Good one there..
I’ve found a different version..But the basic algo somewhere resides in the same neighborhood..:)
int NumberGenerator(unsigned int UB,unsigned int LB)
//LB is the lower bound and UB is the upper bound
{
int x = (rand( ) % UL)+ LL;
return x;
}
int main()
{
int x,LB,UB;
printf(“Upper Bound, Lower Bound?\n”);
scanf(“%d%d”,UB,LB)l
srand(time(NULL));
x = NumberGenerator(UB,LB);
printf (“%i\n”, x);
return NULL;
}
rand() – is b/w 0 and RAND_MAX
rand() % UB – is b/w 0 and UB-1
*Sorry!, a typo in the variable name..Please consider it as UB instead of UL in the NumberGenerator Module..*
(rand() % UB) + LB – is b/w LB and UB..
Logic same?
Hey Bharath, i think there is a minor mistake in the logic of the program posted by you.. Consider LB=2 and UB=9. rand()%UB can generate any number between 0 and 8. Assume that it generates 8. Adding this to LB=2 gives us 10 which is not in [2,9]. I ran the program on my system after correcting a few programming errors. Here are a few runs.
1.netra@netra-laptop:~$ ./a.out
Upper Bound, Lower Bound?
2 9
10
2.netra@netra-laptop:~$ ./a.out
Upper Bound, Lower Bound?
4 8
11
Thanks a lot for ur interest in the post
Hmm..yeah..agreed..i dint try it out..so couldn’t actually verify before posting..
thanks a ton for the solution..!
God Bless..:)
OLD post, but its fun to reply
The program does NOT generates random number. As you must be knowing that present day computers are nothing but a deterministic finite state machine processor, the algorithm used in rand(..) is deterministic too. So it is the seed with which you initialize the libc random number generator (srand()) that is responsible for driving the deterministic algorithm to generate a pseudo-random number and hence it must be random but time(..) is never random, it is very much predictable. If you care about generating pseudo-random numbers which are fairly random for most practical needs, use linux kernel’s random data pool through /dev/random but then again, it is biased to disk activities, network activities etc. but fairly random for day-to-day needs.
thanks for clarifications and suggestions for improvement