| Chapter 3 | Chapter 5 | |||||
index | ||||||
Back to Deley's Homepage | ||||||
| 1. | MTH$RANDOM | |||
| Used by VMS FORTRAN, VMS BASIC, and others. | ||||
| Definition: | ||||
| SEED = (69069*SEED + 1) mod 2**32 | ||||
| X = SEED/2**32 | ||||
| Returns real in range [0,1) {including 0, excluding 1} | ||||
| 2. | RANDU | |||
| Obsolete but still found on some systems | ||||
| Definition: | ||||
| SEED = (65539*SEED) mod 2**31 | ||||
| X = SEED/2**31 | ||||
| Returns real in range [0,1) {including 0, excluding 1} | ||||
| 3. | C and ANSI C | |||
| The ANSI C standard only states that rand() is a random number generator which generates integers between [0,RAND_MAX] inclusive, with RAND_MAX being a value defined in stdlib.h, and RAND_MAX being at least 32767. | ||||
| The rand() function suggested by Brian W. Kernighan and Dennis M. Ritchie in their book "The C programming language" is the one shown below. | ||||
| Definition: | ||||
| #define RAND_MAX 32767 /* (2**15 - 1) */ | ||||
| SEED = (1103515245*SEED + 12345) mod 2**15 | ||||
| X = SEED | ||||
| Returns integer in range [0, RAND_MAX] | ||||
| Note this is not the only random number generator that can be used. Any random number generator can be used. The only stipulation is that RAND_MAX be at least 32767. | ||||
| The rand() generator used by VAX C is almost the same except it uses a modulus of 2**32 instead of 2**15, giving RAND_MAX the value 2147483647. | ||||
| Definition: | ||||
| SEED = (1103515245*SEED + 12345) mod 2**31 | ||||
| X = SEED | ||||
| Returns integer in range [0, 2**31) {including 0, excluding 2**31} | ||||
| 4. | Microsoft C | |||
| The rand() function in the Microsoft C library v4.0. | ||||
| Definition: | ||||
| SEED = (214013*SEED + 2531011) mod 2**31 | ||||
| X = int( SEED/2**16 ) | ||||
| Returns integer in range [0, 2**15) {0 inclusive, 2**15 exclusive} | ||||
| 5. | Turbo Pascal | |||
| The random function in Turbo Pascal v6.0 | ||||
| Definition: | ||||
| SEED = (134775813*SEED + 1) mod 2**32 | ||||
| X = int( SEED/2**16 ) {i.e. the upper 16 bits of SEED} | ||||
| Returns integer in range [0, 2**16) {0 inclusive, 2**16 exclusive} | ||||
| SEED | = (A * SEED + C) mod M |
| X | = f(SEED) |
| 1. | The modulus M is an upper bound on the number of different values SEED can take on. | |
| 2. | If the new value of SEED is the same as one we had before, the sequence will begin to repeat itself in a cyclic manner. | |
| 3. | All sequences generated by a linear congruential formula will eventually enter a cycle which repeats itself endlessly. | |
| i) | C is relatively prime to M; | ||
| ii) | B = (A - 1) is a multiple of P, for every prime P dividing M; | ||
| iii) | B = (A - 1) is a multiple of 4, if M is a multiple of 4. |
| i) | 1 is relatively prime to 2**32 | |
| since 1 is relatively prime to all numbers. | ||
| ii) | 69068 is a multiple of 2, which is the only prime dividing 2**32. | |
| iii) | 69068 is a multiple of 4. | |
| CYCLE | LENGTH OF CYCLE | |
|---|---|---|
| <1> | 536,870,912 | |
| <5> | 536,870,912 | |
| CYCLE | LENGTH OF CYCLE | |
|---|---|---|
| <2> | 268435456 | |
| <4> | 134217728 | |
| <8> | 67108864 | |
| <10> | 268435456 | |
| <16> | 33554432 | |
| <20> | 134217728 | |
| <32> | 16777216 | |
| <40> | 67108864 | |
| <64> | 8388608 | |
| <80> | 33554432 | |
| <128> | 4194304 | |
| <160> | 16777216 | |
| <256> | 2097152 | |
| <320> | 8388608 | |
| <512> | 1048576 | |
| <640> | 4194304 | |
| <1024> | 524288 | |
| <1280> | 2097152 | |
| <2048> | 262144 | |
| <2560> | 1048576 | |
| <4096> | 131072 | |
| <5120> | 524288 | |
| <8192> | 65536 | |
| <10240> | 262144 | |
| <16384> | 32768 | |
| <20480> | 131072 | |
| <32768> | 16384 | |
| <40960> | 65536 | |
| <81920> | 32768 | |
| <163840> | 16384 | |
| --------------- | ||
| Total: | 1073709056 |
| Chapter 3 | Chapter 5 | |||||
index | ||||||
Back to Deley's Homepage | ||||||