CdmRandomGenerator meets UniformRandomBitGenerator named requirements.

[ Merge of http://go/wvgerrit/89766 ]

Certain C++11 (and newer) standard library functions and classes which
utilize random number generators require that the random number
generators meet the specifications of C++11's UniformRandomBitGenerator
name requirements.  This is especially important as C++17 will remove
support non-compliant alternatives.

This change updates CdmRandomGenerator to meet these requirements.

Bug: 143494945
Test: Linux and Android unit tests
Change-Id: Ib6df44da4969ad7596b16d447c3f8bd9864698f6
This commit is contained in:
Alex Dale
2019-11-20 11:55:47 -08:00
parent 84061e93d6
commit 7ab69e7768
3 changed files with 57 additions and 9 deletions

View File

@@ -14,9 +14,18 @@ namespace wvcdm {
// It's purpose is to simplified interface for C++11's <random> library.
// Some of the methods use a "device specific" random seed, if the
// compiler/device does not support device specific randomizers, then the
// actual value supplied may not be random.
// actual value supplied may not be random. The generator is designed to
// meet the C++ named requirement UniformRandomBitGenerator to allow it to
// be used with standard library functions / class which are designed to
// work with the standard library generators.
class CdmRandomGenerator {
public:
// Result type of operator().
using result_type = unsigned int;
// Inclusive boundaries of operator().
static constexpr unsigned int min() { return 0; }
static constexpr unsigned int max() { return RAND_MAX; }
// The maximum number of bytes that can be generated at once for
// `RandomData()`.
static constexpr size_t kMaxRandomDataLength = 8192; // 8 kB
@@ -44,12 +53,11 @@ class CdmRandomGenerator {
// Returns a pseudo-random integer.
// This is similar to `rand()` from the C standard library. The integer
// returned is in the range of [0, RAND_MAX].
int Rand();
// returned is in the range of [min(), max()].
unsigned int Rand();
// Allows for RNG to be callable, this is to make it similar to the
// C++11 generator interfaces.
int operator()() { return Rand(); }
// Allows for RNG to be callable.
unsigned int operator()() { return Rand(); }
// Returns a pseudo-random integer within the provided inclusive range.
uint64_t RandomInRange(uint64_t lower, uint64_t upper);
@@ -79,7 +87,7 @@ class CdmRandomGenerator {
// CdmRandomGenerator.
class CdmRandom {
public:
static int Rand() { return GetInstance()->Rand(); }
static unsigned int Rand() { return GetInstance()->Rand(); }
static uint64_t RandomInRange(uint64_t lower, uint64_t upper) {
return GetInstance()->RandomInRange(lower, upper);
}