| 1 |
// Copyright (c) 2019 Google LLC |
| 2 |
// |
| 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 |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 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. |
| 14 |
|
| 15 |
#include "source/fuzz/pseudo_random_generator.h" |
| 16 |
|
| 17 |
#include <cassert> |
| 18 |
|
| 19 |
namespace spvtools { |
| 20 |
namespace fuzz { |
| 21 |
|
| 22 |
PseudoRandomGenerator::PseudoRandomGenerator(uint32_t seed) : mt_(seed) {} |
| 23 |
|
| 24 |
PseudoRandomGenerator::~PseudoRandomGenerator() = default; |
| 25 |
|
| 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_); |
| 29 |
} |
| 30 |
|
| 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_); |
| 34 |
} |
| 35 |
|
| 36 |
bool PseudoRandomGenerator::RandomBool() { |
| 37 |
return static_cast<bool>(std::uniform_int_distribution<>(0, 1)(mt_)); |
| 38 |
} |
| 39 |
|
| 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); |
| 44 |
} |
| 45 |
|
| 46 |
double PseudoRandomGenerator::RandomDouble() { |
| 47 |
return std::uniform_real_distribution<double>(0.0, 1.0)(mt_); |
| 48 |
} |
| 49 |
|
| 50 |
} // namespace fuzz |
| 51 |
} // namespace spvtools |