diff options
Diffstat (limited to 'src/random.h')
-rw-r--r-- | src/random.h | 12 |
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); } |