mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-29 05:53:21 +02:00
random: add fixed-size std::array
generation
Co-authored-by: Hodlinator <172445034+hodlinator@users.noreply.github.com>
This commit is contained in:
@@ -301,6 +301,15 @@ public:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Generate fixed-size random bytes. */
|
||||||
|
template <size_t N, BasicByte B = std::byte>
|
||||||
|
std::array<B, N> randbytes() noexcept
|
||||||
|
{
|
||||||
|
std::array<B, N> ret;
|
||||||
|
Impl().fillrand(MakeWritableByteSpan(ret));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/** Generate a random 32-bit integer. */
|
/** Generate a random 32-bit integer. */
|
||||||
uint32_t rand32() noexcept { return Impl().template randbits<32>(); }
|
uint32_t rand32() noexcept { return Impl().template randbits<32>(); }
|
||||||
|
|
||||||
|
@@ -58,7 +58,7 @@ BOOST_AUTO_TEST_CASE(fastrandom_tests_deterministic)
|
|||||||
BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());
|
BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());
|
||||||
BOOST_CHECK_EQUAL(ctx1.rand64(), ctx2.rand64());
|
BOOST_CHECK_EQUAL(ctx1.rand64(), ctx2.rand64());
|
||||||
BOOST_CHECK_EQUAL(ctx1.randbits(3), ctx2.randbits(3));
|
BOOST_CHECK_EQUAL(ctx1.randbits(3), ctx2.randbits(3));
|
||||||
BOOST_CHECK(ctx1.randbytes(17) == ctx2.randbytes(17));
|
BOOST_CHECK(std::ranges::equal(ctx1.randbytes<std::byte>(17), ctx2.randbytes<17>())); // check vector/array behavior symmetry
|
||||||
BOOST_CHECK(ctx1.rand256() == ctx2.rand256());
|
BOOST_CHECK(ctx1.rand256() == ctx2.rand256());
|
||||||
BOOST_CHECK_EQUAL(ctx1.randbits(7), ctx2.randbits(7));
|
BOOST_CHECK_EQUAL(ctx1.randbits(7), ctx2.randbits(7));
|
||||||
BOOST_CHECK(ctx1.randbytes(128) == ctx2.randbytes(128));
|
BOOST_CHECK(ctx1.randbytes(128) == ctx2.randbytes(128));
|
||||||
|
Reference in New Issue
Block a user