OR-Tools  8.2
random.cc
Go to the documentation of this file.
1// Copyright 2010-2018 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
34namespace operations_research {
35
37 return absl::uniform_int_distribution<uint32>(0, kuint32max)(generator_);
38}
39
40uint32 ACMRandom::Uniform(uint32 n) { return n == 0 ? 0 : Next() % n; }
41
43 return absl::uniform_int_distribution<uint64>(0, kuint64max)(generator_);
44}
45
47 return val_max == 0 ? 0 : Next64() % val_max;
48}
49
50namespace {
51static inline uint32 Word32At(const char* ptr) {
52 return ((static_cast<uint32>(ptr[0])) + (static_cast<uint32>(ptr[1]) << 8) +
53 (static_cast<uint32>(ptr[2]) << 16) +
54 (static_cast<uint32>(ptr[3]) << 24));
55}
56} // namespace
57
59 char name[PATH_MAX + 20]; // need 12 bytes for 3 'empty' uint32's
60 assert(sizeof(name) - PATH_MAX > sizeof(uint32) * 3);
61
62 if (gethostname(name, PATH_MAX) != 0) {
63 strcpy(name, "default-hostname"); // NOLINT
64 }
65 const int namelen = strlen(name);
66 for (size_t i = 0; i < sizeof(uint32) * 3; ++i) {
67 name[namelen + i] = '\0'; // so we mix 0's once we get to end-of-string
68 }
69#if defined(__GNUC__)
70 uint32 a = getpid();
71 struct timeval tv;
72 gettimeofday(&tv, NULL);
73 uint32 b = static_cast<uint32>((tv.tv_sec + tv.tv_usec) & 0xffffffff);
74#elif defined(_MSC_VER)
75 uint32 a = GetCurrentProcessId();
76 uint32 b = GetTickCount();
77#else // Do not know what to do, returning 0.
78 return 0;
79#endif
80 uint32 c = 0;
81 for (int i = 0; i < namelen; i += sizeof(uint32) * 3) {
82 a += Word32At(name + i);
83 b += Word32At(name + i + sizeof(uint32));
84 c += Word32At(name + i + sizeof(uint32) + sizeof(uint32));
85 mix(a, b, c);
86 }
87 c += namelen; // one final mix
88 mix(a, b, c);
89 return static_cast<int32>(c); // I guess the seed can be negative
90}
91
93
94} // namespace operations_research
static int32 DeterministicSeed()
Definition: random.cc:92
uint32 Uniform(uint32 n)
Definition: random.cc:40
static int32 HostnamePidTimeSeed()
Definition: random.cc:58
const std::string name
unsigned int uint32
int int32
static const uint64 kuint64max
static const uint32 kuint32max
uint64_t uint64
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
static void mix(uint32 &a, uint32 &b, uint32 &c)
Definition: hash.h:28