1 // Copyright (c) 2019 Google LLC
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "source/fuzz/pseudo_random_generator.h"
22 PseudoRandomGenerator::PseudoRandomGenerator(uint32_t seed
) : mt_(seed
) {}
24 PseudoRandomGenerator::~PseudoRandomGenerator() = default;
26 uint32_t PseudoRandomGenerator::RandomUint32(uint32_t bound
) {
27 assert(bound
> 0 && "Bound must be positive");
28 return std::uniform_int_distribution
<uint32_t>(0, bound
- 1)(mt_
);
31 uint64_t PseudoRandomGenerator::RandomUint64(uint64_t bound
) {
32 assert(bound
> 0 && "Bound must be positive");
33 return std::uniform_int_distribution
<uint64_t>(0, bound
- 1)(mt_
);
36 bool PseudoRandomGenerator::RandomBool() {
37 return static_cast<bool>(std::uniform_int_distribution
<>(0, 1)(mt_
));
40 uint32_t PseudoRandomGenerator::RandomPercentage() {
41 // We use 101 because we want a result in the closed interval [0, 100], and
42 // RandomUint32 is not inclusive of its bound.
43 return RandomUint32(101);
46 double PseudoRandomGenerator::RandomDouble() {
47 return std::uniform_real_distribution
<double>(0.0, 1.0)(mt_
);
51 } // namespace spvtools