aboutsummaryrefslogtreecommitdiff
path: root/src/util.h
diff options
context:
space:
mode:
authorGregory Maxwell <greg@xiph.org>2013-02-15 15:27:57 -0800
committerGregory Maxwell <greg@xiph.org>2013-02-18 14:13:39 -0800
commit907a2aa4c78833ce93455567ae10ff2f506e752e (patch)
treeac144e428ed415231c36e6580c2cf9b69d1bd821 /src/util.h
parent2f0fa79db290d5139c27409055b2035099afa6fd (diff)
downloadbitcoin-907a2aa4c78833ce93455567ae10ff2f506e752e.tar.xz
Internal RNG for approximateBestSubset to prevent degenerate behavior.
This fixes test_bitcoin failures on openbsd reported by dhill on IRC. On some systems rand() is a simple LCG over 2^31 and so it produces an even-odd sequence. ApproximateBestSubset was only using the least significant bit and so every run of the iterative solver would be the same for some inputs, resulting in some pretty dumb decisions. Using something other than the least significant bit would paper over the issue but who knows what other way a system's rand() might get us here. Instead we use an internal RNG with a period of something like 2^60 which is well behaved. This also makes it possible to make the selection deterministic for the tests, if we wanted to implement that.
Diffstat (limited to 'src/util.h')
-rw-r--r--src/util.h26
1 files changed, 20 insertions, 6 deletions
diff --git a/src/util.h b/src/util.h
index 97911d7493..5cdca37e8b 100644
--- a/src/util.h
+++ b/src/util.h
@@ -403,13 +403,27 @@ bool SoftSetArg(const std::string& strArg, const std::string& strValue);
*/
bool SoftSetBoolArg(const std::string& strArg, bool fValue);
+/**
+ * MWC RNG of George Marsaglia
+ * This is intended to be fast. It has a period of 2^59.3, though the
+ * least significant 16 bits only have a period of about 2^30.1.
+ *
+ * @return random value
+ */
+extern uint32_t insecure_rand_Rz;
+extern uint32_t insecure_rand_Rw;
+static inline uint32_t insecure_rand(void)
+{
+ insecure_rand_Rz=36969*(insecure_rand_Rz&65535)+(insecure_rand_Rz>>16);
+ insecure_rand_Rw=18000*(insecure_rand_Rw&65535)+(insecure_rand_Rw>>16);
+ return (insecure_rand_Rw<<16)+insecure_rand_Rz;
+}
-
-
-
-
-
-
+/**
+ * Seed insecure_rand using the random pool.
+ * @param Deterministic Use a determinstic seed
+ */
+void seed_insecure_rand(bool fDeterministic=false);
/** Median filter over a stream of values.
* Returns the median of the last N numbers