|  | 
| 22 | 22 | #include <utility> | 
| 23 | 23 | #include <vector> | 
| 24 | 24 | 
 | 
|  | 25 | +#include "absl/random/bit_gen_ref.h" | 
|  | 26 | +#include "absl/random/random.h" | 
| 25 | 27 | #include "absl/strings/str_cat.h" | 
| 26 | 28 | #include "absl/strings/str_join.h" | 
| 27 | 29 | #include "absl/strings/substitute.h" | 
| @@ -126,12 +128,12 @@ void Corpus::Add(const ByteArray& data, const FeatureVec& fv, | 
| 126 | 128 |   weighted_distribution_.AddWeight(ComputeWeight(fv, fs, coverage_frontier)); | 
| 127 | 129 | } | 
| 128 | 130 | 
 | 
| 129 |  | -const CorpusRecord &Corpus::WeightedRandom(size_t random) const { | 
| 130 |  | -  return records_[weighted_distribution_.RandomIndex(random)]; | 
|  | 131 | +const CorpusRecord& Corpus::WeightedRandom(absl::BitGenRef rng) const { | 
|  | 132 | +  return records_[weighted_distribution_.RandomIndex(rng)]; | 
| 131 | 133 | } | 
| 132 | 134 | 
 | 
| 133 |  | -const CorpusRecord &Corpus::UniformRandom(size_t random) const { | 
| 134 |  | -  return records_[random % records_.size()]; | 
|  | 135 | +const CorpusRecord& Corpus::UniformRandom(absl::BitGenRef rng) const { | 
|  | 136 | +  return records_[absl::Uniform<size_t>(rng, 0, records_.size())]; | 
| 135 | 137 | } | 
| 136 | 138 | 
 | 
| 137 | 139 | void Corpus::DumpStatsToFile(const FeatureSet &fs, std::string_view filepath, | 
| @@ -210,18 +212,21 @@ void WeightedDistribution::RecomputeInternalState() { | 
| 210 | 212 | } | 
| 211 | 213 | 
 | 
| 212 | 214 | __attribute__((noinline))  // to see it in profile. | 
| 213 |  | -size_t | 
| 214 |  | -WeightedDistribution::RandomIndex(size_t random) const { | 
|  | 215 | +size_t WeightedDistribution::RandomIndex(absl::BitGenRef rng) const { | 
| 215 | 216 |   FUZZTEST_CHECK(!weights_.empty()); | 
| 216 | 217 |   FUZZTEST_CHECK(cumulative_weights_valid_); | 
| 217 |  | -  uint64_t sum_of_all_weights = cumulative_weights_.back(); | 
| 218 |  | -  if (sum_of_all_weights == 0) | 
| 219 |  | -    return random % size();  // can't do much else here. | 
| 220 |  | -  random = random % sum_of_all_weights; | 
| 221 |  | -  auto it = std::upper_bound(cumulative_weights_.begin(), | 
| 222 |  | -                             cumulative_weights_.end(), random); | 
|  | 218 | +  const uint64_t sum_of_all_weights = cumulative_weights_.back(); | 
|  | 219 | +  if (sum_of_all_weights == 0) { | 
|  | 220 | +    // Can't do much else here. | 
|  | 221 | +    return absl::Uniform<size_t>(rng, 0, size()); | 
|  | 222 | +  } | 
|  | 223 | +  auto it = | 
|  | 224 | +      std::upper_bound(cumulative_weights_.begin(), cumulative_weights_.end(), | 
|  | 225 | +                       absl::Uniform<uint64_t>(rng, 0, sum_of_all_weights)); | 
| 223 | 226 |   FUZZTEST_CHECK(it != cumulative_weights_.end()); | 
| 224 |  | -  return it - cumulative_weights_.begin(); | 
|  | 227 | +  const size_t index = it - cumulative_weights_.begin(); | 
|  | 228 | +  FUZZTEST_CHECK(weights_[index] != 0); | 
|  | 229 | +  return index; | 
| 225 | 230 | } | 
| 226 | 231 | 
 | 
| 227 | 232 | uint64_t WeightedDistribution::PopBack() { | 
|  | 
0 commit comments