UCT Threading: Manage worker threads in a special manager thread
[pachi.git] / random.c
blob6959e3b14373b1aab84620e42086231d7bd9d9d4
1 #include <stdio.h>
3 #include "random.h"
6 /* Simple Park-Miller */
8 static __thread unsigned long pmseed = 29264;
10 void
11 fast_srandom(unsigned long seed_)
13 pmseed = seed_;
16 unsigned long
17 fast_getseed(void)
19 return pmseed;
22 unsigned long
23 fast_random(unsigned int max)
25 unsigned long hi, lo;
26 lo = 16807 * (pmseed & 0xffff);
27 hi = 16807 * (pmseed >> 16);
28 lo += (hi & 0x7fff) << 16;
29 lo += hi >> 15;
30 pmseed = (lo & 0x7fffffff) + (lo >> 31);
31 return ((pmseed & 0xffff) * max) >> 16;
34 float
35 fast_frandom(void)
37 /* Construct (1,2) IEEE float from our random integer */
38 /* http://rgba.org/articles/sfrand/sfrand.htm */
39 union { unsigned long ul; float f; } p;
40 p.ul = (((pmseed *= 16807) & 0x007fffff) - 1) | 0x3f800000;
41 return p.f - 1.0f;