Bitcoin Core  28.1.0
P2P Digital Currency
random.h
Go to the documentation of this file.
1 // Copyright (c) 2023 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_TEST_UTIL_RANDOM_H
6 #define BITCOIN_TEST_UTIL_RANDOM_H
7 
8 #include <consensus/amount.h>
9 #include <random.h>
10 #include <uint256.h>
11 
12 #include <cstdint>
13 
21 
22 enum class SeedRand {
23  ZEROS,
24  SEED,
25 };
26 
29 
30 static inline uint32_t InsecureRand32()
31 {
32  return g_insecure_rand_ctx.rand32();
33 }
34 
35 static inline uint256 InsecureRand256()
36 {
38 }
39 
40 static inline uint64_t InsecureRandBits(int bits)
41 {
42  return g_insecure_rand_ctx.randbits(bits);
43 }
44 
45 static inline uint64_t InsecureRandRange(uint64_t range)
46 {
47  return g_insecure_rand_ctx.randrange(range);
48 }
49 
50 static inline bool InsecureRandBool()
51 {
53 }
54 
56 {
57  return static_cast<CAmount>(InsecureRandRange(MAX_MONEY + 1));
58 }
59 
60 #endif // BITCOIN_TEST_UTIL_RANDOM_H
FastRandomContext g_insecure_rand_ctx
This global and the helpers that use it are not thread-safe.
Definition: random.cpp:14
bool randbool() noexcept
Generate a random boolean.
Definition: random.h:316
static bool InsecureRandBool()
Definition: random.h:50
void SeedRandomForTest(SeedRand seed=SeedRand::SEED)
Seed the RNG for testing.
Definition: random.cpp:18
SeedRand
Definition: random.h:22
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static uint64_t InsecureRandRange(uint64_t range)
Definition: random.h:45
Fast randomness source.
Definition: random.h:376
uint32_t rand32() noexcept
Generate a random 32-bit integer.
Definition: random.h:305
Use (and report) random seed from environment, or a (truly) random one.
static uint256 InsecureRand256()
Definition: random.h:35
256-bit opaque blob.
Definition: uint256.h:178
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
Definition: random.h:204
uint256 rand256() noexcept
generate a random uint256.
Definition: random.h:308
static uint32_t InsecureRand32()
Definition: random.h:30
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
Definition: random.h:254
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:26
static uint64_t InsecureRandBits(int bits)
Definition: random.h:40
Seed with a compile time constant of zeros.
static CAmount InsecureRandMoneyAmount()
Definition: random.h:55