]> www.ginac.de Git - cln.git/blob - src/base/random/cl_UL_random.cc
* All Files have been modified for inclusion of namespace cln;
[cln.git] / src / base / random / cl_UL_random.cc
1 // Word level random number generator.
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cln/random.h"
8
9
10 // Implementation.
11
12 #include "cl_low.h"
13
14 namespace cln {
15
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.
20
21 uint32 random32 (random_state& randomstate)
22 {
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 = 0x5851F42D4C957F2DUL;
27         var uint64 newseed;
28         // multiplizieren, brauche nur letzte 64 Bit:
29         mulu64(seed,a, , newseed =);
30         // c addieren:
31         newseed += 1;
32         // seed neu füllen:
33         randomstate.seed.hi = high32(newseed);
34         randomstate.seed.lo = low32(newseed);
35         // mittlere 32 Bits als Ergebnis:
36         return (uint32)(newseed >> 16);
37
38 #else
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 +=);
50         // c addieren:
51         newseed_lo += 1; if (newseed_lo==0) { newseed_hi += 1; } // um 1 erhöhen
52         // seed neu füllen:
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));
57 #endif
58 }
59
60 }  // namespace cln