-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom.cpp
55 lines (49 loc) · 1.29 KB
/
Random.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "Random.hpp"
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <algorithm>
using Oop::Random;
//------------------------------------------------------------------------------
unsigned int Random::getSeed()
{
bool seed_valid = false;
char *rand_seed_from_env = getenv("RAND_SEED");
unsigned int seed = 0;
if (rand_seed_from_env)
{
std::stringstream rand_seed_sstream(rand_seed_from_env);
rand_seed_sstream >> seed;
if (!rand_seed_sstream.eof() || rand_seed_sstream.bad())
{
seed_valid = false;
std::cerr << "[WARN] could not parse seed for RNG from environment\n";
}
else
{
seed_valid = true;
}
}
if (!seed_valid)
{
std::random_device rand_dev;
seed = rand_dev();
}
return seed;
}
//------------------------------------------------------------------------------
Random& Random::getInstance()
{
static Random instance;
return instance;
}
//------------------------------------------------------------------------------
Random::Random() : mersenne_twister_(getSeed())
{
}
//------------------------------------------------------------------------------
void Random::shufflePickupstack(std::vector<Card*>& pick_up_stack)
{
std::shuffle(std::begin(pick_up_stack), std::end(pick_up_stack),
mersenne_twister_);
}