Make duplicate script compatible with Python 3
[wdl/wdl-ol.git] / WDL / xsrand.h
blob1583272bedccc7241f738406210f2cea65c0bf1f
1 #ifndef _WDL_XSRAND_H_
2 #define _WDL_XSRAND_H_
4 #include "wdltypes.h"
6 class XS64Rand {
7 WDL_UINT64 st;
8 public:
9 XS64Rand(WDL_UINT64 seed) { st=seed; if (!st) st++; }
11 WDL_UINT64 rand64()
13 WDL_UINT64 x=st;
14 x ^= x >> 12; // a
15 x ^= x << 25; // b
16 x ^= x >> 27; // c
17 return (st=x) * WDL_UINT64_CONST(2685821657736338717);
20 void add_entropy(WDL_UINT64 value) { st+=value; if (!st) st++; }
23 class XS1024Rand {
24 WDL_UINT64 st[16];
25 int p;
27 public:
28 XS1024Rand(WDL_UINT64 seed) : p(0)
30 memset(st,0x80,sizeof(st));
31 add_entropy(seed);
33 XS1024Rand(const void *buf, int bufsz) : p(0)
35 memset(st,0x80,sizeof(st));
36 add_entropy(buf,bufsz);
39 WDL_UINT64 rand64()
41 WDL_UINT64 s0 = st[ p ], *wr = st + (p = (p+1)&15), s1 = *wr;
42 s1 ^= s1 << 31; // a
43 s1 ^= s1 >> 11; // b
44 s0 ^= s0 >> 30; // c
45 return ( *wr = s0 ^ s1 ) * WDL_UINT64_CONST(1181783497276652981);
48 void add_entropy(WDL_UINT64 value)
50 XS64Rand r(value);
51 for (int x=0;x<16;x++) st[x]+=r.rand64();
53 void add_entropy(const void *buf, int bufsz)
55 unsigned char *wr = (unsigned char *)st;
56 if (bufsz > (int) sizeof(st)) bufsz = (int) sizeof(st);
57 if (buf)
59 const unsigned char *p = (const unsigned char *)buf;
60 while (bufsz-->0) *wr++ += *p++;
66 #endif