| GENERATING RANDOM INTEGERS WITHIN A DESIRED RANGE | ADDITIVE CONGRUENTIAL RANDOM NUMBER GENERATORS | |||
index | ||||
Back to Deley's Homepage | ||||
| int seed; | /* Choose any integer value for seed to initialize the random number generator with. The same value of seed will always produce the same sequence of random numbers. To get a different sequence of random numbers next time, choose a different value of seed. | ||
| Note: on Microsoft Visual C++ 6.0, if seed is negative than a seed value from the system clock is used. Also check for a randomize() function on your compiler. */ | |||
| double r; | /* random value in range [0,1) */ | ||
| long int M; | /* user supplied upper boundary */ | ||
| double x; | /* random value in range [0,M) */ | ||
| int y; | /* random integer in range [0,M) if M is an integer then range = [0,M-1] */ | ||
| int z; | /* random integer in range [1,M+1) if M is an integer then range = [1,M] */ | ||
| int count; | /* just a variable we need to count how many random numbers we've made for this example */ | ||
| /*BEGIN CODE*/ | |||
| seed = 10000; | /* choose a seed value */ | ||
| srand(seed); | /*initialize random number generator*/ | ||
| M = 1000; | /* Choose M. Upper bound */ | ||
| for(count=1; count<=20; ++count) | |
| { |
| r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) ); |
| /* r is a random floating point value in the range [0,1) {including 0, not including 1}. Note we must convert rand() and/or RAND_MAX+1 to floating point values to avoid integer division. In addition, Sean Scanlon pointed out the possibility that RAND_MAX may be the largest positive integer the architecture can represent, so (RAND_MAX+1) may result in an overflow, or more likely the value will end up being the largest negative integer the architecture can represent, so to avoid this we convert RAND_MAX and 1 to doubles before adding. */ |
| x = (r * M); |
| /* x is a random floating point value in the range [0,M) {including 0, not including M}. */ |
| y = (int) x; |
| /* y is a random integer in the range [0,M) {including 0, not including M}. If M is an integer then the range is [0,M-1] {inclusive} */ |
| z = y + 1; |
| /* z is a random integer in the range [1,M+1) {including 1, not including M+1}. If M is an integer then the range is [1,M] {inclusive} */ |
| printf("random number %3d %5f %5f %5d %5d\n",count,r,x,y,z); |
| } |







| GENERATING RANDOM INTEGERS WITHIN A DESIRED RANGE | ADDITIVE CONGRUENTIAL RANDOM NUMBER GENERATORS | |||
index | ||||
Back to Deley's Homepage | ||||