File tree Expand file tree Collapse file tree 2 files changed +16
-16
lines changed Expand file tree Collapse file tree 2 files changed +16
-16
lines changed Original file line number Diff line number Diff line change 14
14
#include < wincrypt.h>
15
15
#endif
16
16
#include < logging.h> // for LogPrintf()
17
+ #include < randomenv.h>
18
+ #include < support/allocators/secure.h>
17
19
#include < sync.h> // for Mutex
18
20
#include < util/time.h> // for GetTimeMicros()
19
21
20
22
#include < stdlib.h>
21
23
#include < thread>
22
24
23
- #include < randomenv.h>
24
-
25
- #include < support/allocators/secure.h>
26
-
27
25
#ifndef WIN32
28
26
#include < fcntl.h>
29
27
#include < sys/time.h>
@@ -587,16 +585,6 @@ uint64_t GetRand(uint64_t nMax) noexcept
587
585
return FastRandomContext (g_mock_deterministic_tests).randrange (nMax);
588
586
}
589
587
590
- std::chrono::microseconds GetRandMicros (std::chrono::microseconds duration_max) noexcept
591
- {
592
- return std::chrono::microseconds{GetRand (duration_max.count ())};
593
- }
594
-
595
- std::chrono::milliseconds GetRandMillis (std::chrono::milliseconds duration_max) noexcept
596
- {
597
- return std::chrono::milliseconds{GetRand (duration_max.count ())};
598
- }
599
-
600
588
int GetRandInt (int nMax) noexcept
601
589
{
602
590
return GetRand (nMax);
Original file line number Diff line number Diff line change 67
67
* Thread-safe.
68
68
*/
69
69
void GetRandBytes (unsigned char * buf, int num) noexcept ;
70
+ /* * Generate a uniform random integer in the range [0..range). Precondition: range > 0 */
70
71
uint64_t GetRand (uint64_t nMax) noexcept ;
71
- std::chrono::microseconds GetRandMicros (std::chrono::microseconds duration_max) noexcept ;
72
- std::chrono::milliseconds GetRandMillis (std::chrono::milliseconds duration_max) noexcept ;
72
+ /* * Generate a uniform random duration in the range [0..max). Precondition: max.count() > 0 */
73
+ template <typename D>
74
+ D GetRandomDuration (typename std::common_type<D>::type max) noexcept
75
+ // Having the compiler infer the template argument from the function argument
76
+ // is dangerous, because the desired return value generally has a different
77
+ // type than the function argument. So std::common_type is used to force the
78
+ // call site to specify the type of the return value.
79
+ {
80
+ assert (max.count () > 0 );
81
+ return D{GetRand (max.count ())};
82
+ };
83
+ constexpr auto GetRandMicros = GetRandomDuration<std::chrono::microseconds>;
84
+ constexpr auto GetRandMillis = GetRandomDuration<std::chrono::milliseconds>;
73
85
int GetRandInt (int nMax) noexcept ;
74
86
uint256 GetRandHash () noexcept ;
75
87
You can’t perform that action at this time.
0 commit comments