diff options
| author | Pieter Wuille <[email protected]> | 2017-02-25 12:16:58 -0800 |
|---|---|---|
| committer | Pieter Wuille <[email protected]> | 2017-03-29 11:26:08 -0700 |
| commit | 4fd2d2fc97e21efceab849576e544160fd5e3e3d (patch) | |
| tree | 4d36745b0b5a72bf51b2e54bfd13151920a2c57b /src/random.h | |
| parent | Switch FastRandomContext to ChaCha20 (diff) | |
| download | discoin-4fd2d2fc97e21efceab849576e544160fd5e3e3d.tar.xz discoin-4fd2d2fc97e21efceab849576e544160fd5e3e3d.zip | |
Add a FastRandomContext::randrange and use it
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 077f58c4d..9551e1c46 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); } |