mq_nodup(): Merge tags when removing moves. Clean up.
[pachi/pachi-r6144.git] / random.c
blobf488f968e2aaa6a107c706dcb9ce80a11a41b768
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 uint16_t
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;