diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-05-22 17:15:40 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-05-22 17:15:40 +0000 |
commit | ce793e31f387c0715fa5b50c20e06510cc3e95ff (patch) | |
tree | 5b15972bdcbe7a26933e8158f52b29fb014dc421 /src/stable/randomcmwc.cpp | |
parent | 2295579eb790d6eff6e54e84c01da6de10809a71 (diff) | |
download | libbu++-ce793e31f387c0715fa5b50c20e06510cc3e95ff.tar.gz libbu++-ce793e31f387c0715fa5b50c20e06510cc3e95ff.tar.bz2 libbu++-ce793e31f387c0715fa5b50c20e06510cc3e95ff.tar.xz libbu++-ce793e31f387c0715fa5b50c20e06510cc3e95ff.zip |
Moved random to stable, just needs some minor tweaks. But it's already in use
in a couple of core components, including in tempFile name generation.
Diffstat (limited to 'src/stable/randomcmwc.cpp')
-rw-r--r-- | src/stable/randomcmwc.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/stable/randomcmwc.cpp b/src/stable/randomcmwc.cpp new file mode 100644 index 0000000..a4e807e --- /dev/null +++ b/src/stable/randomcmwc.cpp | |||
@@ -0,0 +1,52 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2012 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/randomcmwc.h" | ||
9 | |||
10 | #define PHI 0x9e3779b9 | ||
11 | |||
12 | Bu::RandomCmwc::RandomCmwc() : | ||
13 | q( 0 ), | ||
14 | c( 362436 ) | ||
15 | { | ||
16 | q = new uint32_t[4096]; | ||
17 | } | ||
18 | |||
19 | Bu::RandomCmwc::~RandomCmwc() | ||
20 | { | ||
21 | delete[] q; | ||
22 | } | ||
23 | |||
24 | void Bu::RandomCmwc::seed( int32_t iSeed ) | ||
25 | { | ||
26 | int i; | ||
27 | |||
28 | q[0] = iSeed; | ||
29 | q[1] = iSeed + PHI; | ||
30 | q[2] = iSeed + PHI + PHI; | ||
31 | |||
32 | for (i = 3; i < 4096; i++) | ||
33 | q[i] = q[i - 3] ^ q[i - 2] ^ PHI ^ i; | ||
34 | } | ||
35 | |||
36 | int32_t Bu::RandomCmwc::rand() | ||
37 | { | ||
38 | uint64_t t, a = 18782LL; | ||
39 | static uint32_t i = 4095; | ||
40 | uint32_t x, r = 0xfffffffe; | ||
41 | i = (i + 1) & 4095; | ||
42 | t = a * q[i] + c; | ||
43 | c = (t >> 32); | ||
44 | x = t + c; | ||
45 | if( x < c ) | ||
46 | { | ||
47 | x++; | ||
48 | c++; | ||
49 | } | ||
50 | return (q[i] = r - x); | ||
51 | } | ||
52 | |||