1/**
2 * Copyright (c) Glow Contributors. See CONTRIBUTORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "glow/Support/Random.h"
18#include "llvm/Support/CommandLine.h"
19
20#include <cassert>
21
22static llvm::cl::opt<glow::PseudoRNG::result_type>
23 pseudoRandomSeed("pseudo-random-seed",
24 llvm::cl::desc("Seed for pseudo-random numbers"),
25 llvm::cl::init(glow::PseudoRNG::Engine::default_seed));
26
27namespace glow {
28
29PseudoRNG::PseudoRNG() : engine_(pseudoRandomSeed.getValue()) {}
30
31// Constant uniform distribution used as a template in nextRand.
32// This computes the parameters once instead of on each call.
33const static std::uniform_real_distribution<> uniformReal(-1, 1);
34
35double PseudoRNG::nextRand() {
36 return std::uniform_real_distribution<double>(uniformReal.param())(engine_);
37}
38
39double PseudoRNG::nextRandReal(double a, double b) {
40 return std::uniform_real_distribution<double>(a, b)(engine_);
41}
42
43} // namespace glow
44