Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / tools / fuzzing / common / FuzzingTraits.cpp
blob9e6ba3ac1dedfe92daf8746f373936e9fdcdc2ac
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include <prinrval.h>
8 #include <thread>
9 #include <mutex>
10 #include "FuzzingTraits.h"
12 namespace mozilla {
13 namespace fuzzing {
15 /* static */
16 unsigned int FuzzingTraits::Random(unsigned int aMax) {
17 MOZ_ASSERT(aMax > 0, "aMax needs to be bigger than 0");
18 std::uniform_int_distribution<unsigned int> d(0, aMax);
19 return d(Rng());
22 /* static */
23 bool FuzzingTraits::Sometimes(unsigned int aProbability) {
24 return FuzzingTraits::Random(aProbability) == 0;
27 /* static */
28 size_t FuzzingTraits::Frequency(const size_t aSize, const uint64_t aFactor) {
29 return RandomIntegerRange<size_t>(0, ceil(float(aSize) / aFactor)) + 1;
32 /* static */
33 std::mt19937_64& FuzzingTraits::Rng() {
34 static std::mt19937_64 rng;
35 static std::once_flag flag;
36 std::call_once(flag, [&] { rng.seed(PR_IntervalNow()); });
37 return rng;
40 } // namespace fuzzing
41 } // namespace mozilla