1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
media / learning / impl / random_number_generator.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/learning/impl/random_number_generator.h"
#include <limits>
#include "base/rand_util.h"
namespace media {
class BaseRandomNumberGenerator : public RandomNumberGenerator {
public:
uint64_t Generate() override { return base::RandUint64(); }
protected:
~BaseRandomNumberGenerator() override = default;
};
// static
RandomNumberGenerator* RandomNumberGenerator::Default() {
static BaseRandomNumberGenerator* rng = nullptr;
// TODO(liberato): locking?
if (!rng)
rng = new BaseRandomNumberGenerator();
return rng;
}
uint64_t RandomNumberGenerator::Generate(uint64_t range) {
// Don't just % generate(), since that wouldn't be uniform anymore.
// This is copied from base/rand_util.cc .
uint64_t max_acceptable_value =
(std::numeric_limits<uint64_t>::max() / range) * range - 1;
uint64_t value;
do {
value = Generate();
} while (value > max_acceptable_value);
return value % range;
}
double RandomNumberGenerator::GenerateDouble(double range) {
return base::BitsToOpenEndedUnitInterval(Generate()) * range;
}
HasRandomNumberGenerator::HasRandomNumberGenerator(RandomNumberGenerator* rng)
: rng_(rng ? rng : RandomNumberGenerator::Default()) {}
HasRandomNumberGenerator::~HasRandomNumberGenerator() = default;
void HasRandomNumberGenerator::SetRandomNumberGeneratorForTesting(
RandomNumberGenerator* rng) {
rng_ = rng;
}
} // namespace media