diff options
| author | Eli Zaretskii | 2016-12-31 13:01:19 +0200 |
|---|---|---|
| committer | Eli Zaretskii | 2016-12-31 13:01:19 +0200 |
| commit | a78a167a4f74759fd291802b95193a0f39a4cb59 (patch) | |
| tree | c75e322708f26523e6cdd0b30a48c48cff670478 /src | |
| parent | 3b25ffa7eb59e12834a2f5155c97a9104750ecd1 (diff) | |
| download | emacs-a78a167a4f74759fd291802b95193a0f39a4cb59.tar.gz emacs-a78a167a4f74759fd291802b95193a0f39a4cb59.zip | |
Serialize random number generation on MS-Windows
* src/w32.c (rand_as183): New function.
(random): Use it instead of MS runtime's 'rand'. This avoids
producing separate and identical random series in each Lisp
thread.
(srandom): Modify to supply 3 seed values to 'rand_as183'.
Diffstat (limited to 'src')
| -rw-r--r-- | src/w32.c | 27 |
1 files changed, 25 insertions, 2 deletions
| @@ -2154,17 +2154,40 @@ w32_init_random (void *buf, ptrdiff_t buflen) | |||
| 2154 | return -1; | 2154 | return -1; |
| 2155 | } | 2155 | } |
| 2156 | 2156 | ||
| 2157 | /* MS-Windows 'rand' produces separate identical series for each | ||
| 2158 | thread, so we replace it with our version. */ | ||
| 2159 | |||
| 2160 | /* Algorithm AS183: An Efficient and Portable Pseudo-random Number | ||
| 2161 | Generator, by B.A. Wichmann, I.D. Hill. AS, v31, No. 2 (1982). */ | ||
| 2162 | static int ix = 3172, iy = 9814, iz = 20125; | ||
| 2163 | #define RAND_MAX_X 30269 | ||
| 2164 | #define RAND_MAX_Y 30307 | ||
| 2165 | #define RAND_MAX_Z 30323 | ||
| 2166 | |||
| 2167 | static int | ||
| 2168 | rand_as183 (void) | ||
| 2169 | { | ||
| 2170 | ix = (171 * ix) % RAND_MAX_X; | ||
| 2171 | iy = (172 * iy) % RAND_MAX_Y; | ||
| 2172 | iz = (170 * iz) % RAND_MAX_Z; | ||
| 2173 | |||
| 2174 | return (ix + iy + iz) & 0x7fff; | ||
| 2175 | } | ||
| 2176 | |||
| 2157 | int | 2177 | int |
| 2158 | random (void) | 2178 | random (void) |
| 2159 | { | 2179 | { |
| 2160 | /* rand () on NT gives us 15 random bits...hack together 30 bits. */ | 2180 | /* rand_as183 () gives us 15 random bits...hack together 30 bits. */ |
| 2161 | return ((rand () << 15) | rand ()); | 2181 | return ((rand_as183 () << 15) | rand_as183 ()); |
| 2162 | } | 2182 | } |
| 2163 | 2183 | ||
| 2164 | void | 2184 | void |
| 2165 | srandom (int seed) | 2185 | srandom (int seed) |
| 2166 | { | 2186 | { |
| 2167 | srand (seed); | 2187 | srand (seed); |
| 2188 | ix = rand () % RAND_MAX_X; | ||
| 2189 | iy = rand () % RAND_MAX_Y; | ||
| 2190 | iz = rand () % RAND_MAX_Z; | ||
| 2168 | } | 2191 | } |
| 2169 | 2192 | ||
| 2170 | /* Return the maximum length in bytes of a multibyte character | 2193 | /* Return the maximum length in bytes of a multibyte character |