forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.h
77 lines (62 loc) · 2.24 KB
/
random.h
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2010-2021 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef OR_TOOLS_BASE_RANDOM_H_
#define OR_TOOLS_BASE_RANDOM_H_
#include <random>
#include <string>
#include "absl/random/random.h"
#include "ortools/base/basictypes.h"
namespace operations_research {
// A wrapper around std::mt19937. Called ACMRandom for historical reasons.
class ACMRandom {
public:
explicit ACMRandom(int32_t seed) : generator_(seed) {}
// Unbounded generators.
uint32_t Next();
uint64_t Next64();
uint64_t Rand64() { return Next64(); }
uint64_t operator()() { return Next64(); }
// Bounded generators.
uint32_t Uniform(uint32_t n);
uint64_t operator()(uint64_t val_max);
// Seed management.
void Reset(int32_t seed) { generator_.seed(seed); }
static int32_t HostnamePidTimeSeed();
static int32_t DeterministicSeed();
// C++11 goodies.
typedef int64_t difference_type;
typedef uint64_t result_type;
static constexpr result_type min() { return 0; }
static constexpr result_type max() { return kuint32max; }
private:
std::mt19937 generator_;
};
class MTRandom : public ACMRandom {
public:
explicit MTRandom(int32_t seed) : ACMRandom(seed) {}
// MTRandom also supports a std::string seed.
explicit MTRandom(const std::string& str_seed)
: ACMRandom(GenerateInt32SeedFromString(str_seed)) {}
MTRandom() : ACMRandom(ACMRandom::HostnamePidTimeSeed()) {}
private:
int32_t GenerateInt32SeedFromString(const std::string& str) {
uint32_t seed = 1234567;
for (size_t i = 0; i < str.size(); ++i) {
seed *= 1000003; // prime
seed += static_cast<uint32_t>(str[i]);
}
return seed >> 1; // Will fit into an int32_t.
}
};
} // namespace operations_research
#endif // OR_TOOLS_BASE_RANDOM_H_