1
//===-- RandomNumberGenerator.cpp - Implement RNG class -------------------===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
// This file implements deterministic random number generation (RNG).
11
// The current implementation is NOT cryptographically secure as it uses
12
// the C++11 <random> facilities.
14
//===----------------------------------------------------------------------===//
16
#include "llvm/Support/RandomNumberGenerator.h"
17
#include "llvm/Support/CommandLine.h"
18
#include "llvm/Support/Debug.h"
19
#include "llvm/Support/raw_ostream.h"
23
#define DEBUG_TYPE "rng"
25
// Tracking BUG: 19665
26
// http://llvm.org/bugs/show_bug.cgi?id=19665
28
// Do not change to cl::opt<uint64_t> since this silently breaks argument parsing.
29
static cl::opt<unsigned long long>
30
Seed("rng-seed", cl::value_desc("seed"),
31
cl::desc("Seed for the random number generator"), cl::init(0));
33
RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
36
dbgs() << "Warning! Using unseeded random number generator.\n"
39
// Combine seed and salts using std::seed_seq.
40
// Data: Seed-low, Seed-high, Salt
41
// Note: std::seed_seq can only store 32-bit values, even though we
42
// are using a 64-bit RNG. This isn't a problem since the Mersenne
43
// twister constructor copies these correctly into its initial state.
44
std::vector<uint32_t> Data;
45
Data.reserve(2 + Salt.size());
47
Data.push_back(Seed >> 32);
49
std::copy(Salt.begin(), Salt.end(), Data.end());
51
std::seed_seq SeedSeq(Data.begin(), Data.end());
52
Generator.seed(SeedSeq);
55
uint_fast64_t RandomNumberGenerator::operator()() {