RNG API
Get a random number
1
Function uint64_t database::get_random_bits(uint64_t bound)
Copied!
Parameters
Returns
bound: The upper limit for the random number.
A random number within the bound range.

TypeDefs

1
peerplays/libraries/chain/include/graphene/chain/protocol/types.hpp
2
typedef fc::ripemd160 secret_hash_type;
3
4
5
peerplays/libraries/fc/include/fc/crypto/hash_ctr_rng.hpp
6
template<class HashClass, int SeedLength>
7
8
class hash_ctr_rng
9
{...}
Copied!

Declaration

1
peerplays/libraries/chain/include/graphene/chain/database.hpp
2
3
fc::hash_ctr_rng<secret_hash_type, 20> _random_number_generator;
Copied!

Examples

Initialization in constructor

1
peerplays/libraries/chain/db_management.cpp
2
3
_random_number_generator(fc::ripemd160().data())
Copied!

Updating seed on a new block

1
peerplays/libraries/chain/db_update.cpp
2
3
modify( _dgp, [&]( dynamic_global_property_object& dgp ){
4
secret_hash_type::encoder enc;
5
fc::raw::pack( enc, dgp.random );
6
fc::raw::pack( enc, b.previous_secret );
7
dgp.random = enc.result();
8
_random_number_generator = fc::hash_ctr_rng<secret_hash_type, 20>(dgp.random.data());
Copied!

Getting a new random number

1
peerplays/libraries/chain/db_update.cpp
2
3
uint64_t database::get_random_bits( uint64_t bound )
4
{
5
return _random_number_generator(bound);
6
}
7
8
peerplays/libraries/fc/include/fc/crypto/hash_ctr_rng.hpp
9
10
uint64_t operator()( uint64_t bound )
11
{
12
if( bound <= 1 )
13
return 0;
14
uint8_t bitcount = boost::multiprecision::detail::find_msb( bound ) + 1;
15
// probability of loop exiting is >= 1/2, so probability of
16
// running N times is bounded above by (1/2)^N
17
while( true )
18
{
19
uint64_t result = get_bits( bitcount );
20
if( result < bound )
21
return result;
22
}
23
}
Copied!
Last modified 1yr ago
Export as PDF
Copy link