aboutsummaryrefslogtreecommitdiff
path: root/src/random.h
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2017-02-25 12:16:58 -0800
committerPieter Wuille <pieter.wuille@gmail.com>2017-03-29 11:26:08 -0700
commit4fd2d2fc97e21efceab849576e544160fd5e3e3d (patch)
tree4d36745b0b5a72bf51b2e54bfd13151920a2c57b /src/random.h
parent16329224e70d0525208f6b0ba00c5e1531a4f5ea (diff)
Add a FastRandomContext::randrange and use it
Diffstat (limited to 'src/random.h')
-rw-r--r--src/random.h12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/random.h b/src/random.h
index 077f58c4d9..9551e1c461 100644
--- a/src/random.h
+++ b/src/random.h
@@ -7,6 +7,7 @@
#define BITCOIN_RANDOM_H
#include "crypto/chacha20.h"
+#include "crypto/common.h"
#include "uint256.h"
#include <stdint.h>
@@ -91,6 +92,17 @@ public:
}
}
+ /** Generate a random integer in the range [0..range). */
+ uint64_t randrange(uint64_t range)
+ {
+ --range;
+ int bits = CountBits(range);
+ while (true) {
+ uint64_t ret = randbits(bits);
+ if (ret <= range) return ret;
+ }
+ }
+
/** Generate a random 32-bit integer. */
uint32_t rand32() { return randbits(32); }