1 // Word level random number generator.
7 #include "cln/random.h"
16 // Zufallszahlengenerator nach [Knuth: The Art of Computer Programming, Vol. II,
17 // Seminumerical Algorithms, 3.3.4., Table 1, Line 30], nach C. Haynes:
18 // X eine 64-Bit-Zahl. Iteration X := (a*X+c) mod m
19 // mit m=2^64, a=6364136223846793005, c=1.
21 uint32 random32 (random_state& randomstate)
23 #ifdef HAVE_FAST_LONGLONG
24 // Multiplikator a=6364136223846793005 = 0x5851F42D4C957F2D :
25 var uint64 seed = highlow64(randomstate.seed.hi,randomstate.seed.lo);
26 var const uint64 a = 0x5851F42D4C957F2DULL;
28 // multiplizieren, brauche nur letzte 64 Bit:
29 mulu64(seed,a, , newseed =);
33 randomstate.seed.hi = high32(newseed);
34 randomstate.seed.lo = low32(newseed);
35 // mittlere 32 Bits als Ergebnis:
36 return (uint32)(newseed >> 16);
39 // Multiplikator a=6364136223846793005 = 0x5851F42D4C957F2D :
40 var uint32 seed_hi = randomstate.seed.hi;
41 var uint32 seed_lo = randomstate.seed.lo;
42 var const uint32 a_hi = 0x5851F42D;
43 var const uint32 a_lo = 0x4C957F2D;
44 var uint32 newseed_hi;
45 var uint32 newseed_lo;
46 // multiplizieren, brauche nur letzte 64 Bit:
47 mulu32(seed_lo,a_lo, newseed_hi =, newseed_lo =);
48 mulu32(seed_lo,a_hi, , newseed_hi +=);
49 mulu32(seed_hi,a_lo, , newseed_hi +=);
51 newseed_lo += 1; if (newseed_lo==0) { newseed_hi += 1; } // um 1 erhöhen
53 randomstate.seed.hi = newseed_hi;
54 randomstate.seed.lo = newseed_lo;
55 // mittlere 32 Bits als Ergebnis:
56 return highlow32(low16(newseed_hi),high16(newseed_lo));