| Line | |
|---|
| 1 | /* |
|---|
| 2 | Copyright (C) 2002 Luc Van Oostenryck |
|---|
| 3 | |
|---|
| 4 | This is free software. You can redistribute and |
|---|
| 5 | modify it under the terms of the GNU General Public |
|---|
| 6 | Public License. |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | #include <stdlib.h> |
|---|
| 10 | #include <stdint.h> |
|---|
| 11 | |
|---|
| 12 | /* Knuth's TAOCP section 3.6 */ |
|---|
| 13 | #define M ((1U<<31) -1) |
|---|
| 14 | #define A 48271 |
|---|
| 15 | #define Q 44488 // M/A |
|---|
| 16 | #define R 3399 // M%A; R < Q !!! |
|---|
| 17 | |
|---|
| 18 | // FIXME: ISO C/SuS want a longer period |
|---|
| 19 | |
|---|
| 20 | int rand_r(unsigned int* seed) |
|---|
| 21 | { int32_t X; |
|---|
| 22 | |
|---|
| 23 | X = *seed; |
|---|
| 24 | X = A*(X%Q) - R * (int32_t) (X/Q); |
|---|
| 25 | if (X < 0) |
|---|
| 26 | X += M; |
|---|
| 27 | |
|---|
| 28 | *seed = X; |
|---|
| 29 | return X; |
|---|
| 30 | } |
|---|
Note: See
TracBrowser
for help on using the repository browser.