Bitcoin Core  27.99.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 
22 
26 extern bool g_mock_deterministic_tests;
27 
28 enum class SeedRand {
29  ZEROS,
30  SEED,
31 };
32 
34 void Seed(FastRandomContext& ctx);
35 
36 static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
37 {
38  if (seed == SeedRand::ZEROS) {
39  g_insecure_rand_ctx = FastRandomContext(/*fDeterministic=*/true);
40  } else {
42  }
43 }
44 
45 static inline uint32_t InsecureRand32()
46 {
47  return g_insecure_rand_ctx.rand32();
48 }
49 
50 static inline uint256 InsecureRand256()
51 {
53 }
54 
55 static inline uint64_t InsecureRandBits(int bits)
56 {
57  return g_insecure_rand_ctx.randbits(bits);
58 }
59 
60 static inline uint64_t InsecureRandRange(uint64_t range)
61 {
62  return g_insecure_rand_ctx.randrange(range);
63 }
64 
65 static inline bool InsecureRandBool()
66 {
68 }
69 
71 {
72  return static_cast<CAmount>(InsecureRandRange(MAX_MONEY + 1));
73 }
74 
75 #endif // BITCOIN_TEST_UTIL_RANDOM_H
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:26
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
Fast randomness source.
Definition: random.h:145
uint32_t rand32() noexcept
Generate a random 32-bit integer.
Definition: random.h:222
uint256 rand256() noexcept
generate a random uint256.
Definition: random.cpp:664
bool randbool() noexcept
Generate a random boolean.
Definition: random.h:228
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
Definition: random.h:185
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:203
256-bit opaque blob.
Definition: uint256.h:106
SeedRand
Definition: random.h:28
@ ZEROS
Seed with a compile time constant of zeros.
@ SEED
Call the Seed() helper.
bool g_mock_deterministic_tests
Flag to make GetRand in random.h return the same number.
Definition: random.cpp:643
static CAmount InsecureRandMoneyAmount()
Definition: random.h:70
static uint64_t InsecureRandRange(uint64_t range)
Definition: random.h:60
static uint256 InsecureRand256()
Definition: random.h:50
static void SeedInsecureRand(SeedRand seed=SeedRand::SEED)
Definition: random.h:36
void Seed(FastRandomContext &ctx)
Seed the given random ctx or use the seed passed in via an environment var.
Definition: random.cpp:24
static uint64_t InsecureRandBits(int bits)
Definition: random.h:55
static uint32_t InsecureRand32()
Definition: random.h:45
FastRandomContext g_insecure_rand_ctx
This global and the helpers that use it are not thread-safe.
Definition: random.cpp:14
static bool InsecureRandBool()
Definition: random.h:65