OR-Tools  9.0
random.cc
Go to the documentation of this file.
1 // Copyright 2010-2021 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #if defined(__GNUC__)
15 #include <unistd.h>
16 #if defined(__linux__)
17 #include <linux/limits.h>
18 #endif
19 #endif
20 #if defined(_MSC_VER)
21 #include <Winsock2.h>
22 #include <windows.h>
23 #define PATH_MAX 4096
24 #else
25 #include <sys/time.h>
26 #endif
27 
28 #include <cstring>
29 #include <ctime>
30 
31 #include "ortools/base/hash.h"
32 #include "ortools/base/random.h"
33 
34 namespace operations_research {
35 
36 uint32_t ACMRandom::Next() {
37  return absl::uniform_int_distribution<uint32_t>(0, kuint32max)(generator_);
38 }
39 
40 uint32_t ACMRandom::Uniform(uint32_t n) { return n == 0 ? 0 : Next() % n; }
41 
42 uint64_t ACMRandom::Next64() {
43  return absl::uniform_int_distribution<uint64_t>(0, kuint64max)(generator_);
44 }
45 
46 uint64_t ACMRandom::operator()(uint64_t val_max) {
47  return val_max == 0 ? 0 : Next64() % val_max;
48 }
49 
50 namespace {
51 static inline uint32_t Word32At(const char* ptr) {
52  return ((static_cast<uint32_t>(ptr[0])) +
53  (static_cast<uint32_t>(ptr[1]) << 8) +
54  (static_cast<uint32_t>(ptr[2]) << 16) +
55  (static_cast<uint32_t>(ptr[3]) << 24));
56 }
57 } // namespace
58 
60  char name[PATH_MAX + 20]; // need 12 bytes for 3 'empty' uint32_t's
61  assert(sizeof(name) - PATH_MAX > sizeof(uint32_t) * 3);
62 
63  if (gethostname(name, PATH_MAX) != 0) {
64  strcpy(name, "default-hostname"); // NOLINT
65  }
66  const int namelen = strlen(name);
67  for (size_t i = 0; i < sizeof(uint32_t) * 3; ++i) {
68  name[namelen + i] = '\0'; // so we mix 0's once we get to end-of-string
69  }
70 #if defined(__GNUC__)
71  uint32_t a = getpid();
72  struct timeval tv;
73  gettimeofday(&tv, NULL);
74  uint32_t b = static_cast<uint32_t>((tv.tv_sec + tv.tv_usec) & 0xffffffff);
75 #elif defined(_MSC_VER)
76  uint32_t a = GetCurrentProcessId();
77  uint32_t b = GetTickCount();
78 #else // Do not know what to do, returning 0.
79  return 0;
80 #endif
81  uint32_t c = 0;
82  for (int i = 0; i < namelen; i += sizeof(uint32_t) * 3) {
83  a += Word32At(name + i);
84  b += Word32At(name + i + sizeof(uint32_t));
85  c += Word32At(name + i + sizeof(uint32_t) + sizeof(uint32_t));
86  mix(a, b, c);
87  }
88  c += namelen; // one final mix
89  mix(a, b, c);
90  return static_cast<int32_t>(c); // I guess the seed can be negative
91 }
92 
93 int32_t ACMRandom::DeterministicSeed() { return 0; }
94 
95 } // namespace operations_research
static int32_t HostnamePidTimeSeed()
Definition: random.cc:59
static int32_t DeterministicSeed()
Definition: random.cc:93
uint32_t Uniform(uint32_t n)
Definition: random.cc:40
int64_t b
int64_t a
const std::string name
static const uint64_t kuint64max
static const uint32_t kuint32max
Collection of objects used to extend the Constraint Solver library.
static void mix(uint32_t &a, uint32_t &b, uint32_t &c)
Definition: hash.h:28