Repository navigation
Expand file tree
/
Copy pathlua_random.cc
More file actions
149 lines (129 loc) · 4.47 KB
/
Copy pathlua_random.cc
File metadata and controls
149 lines (129 loc) · 4.47 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (C) 2016 Google Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
////////////////////////////////////////////////////////////////////////////////
#include "deepmind/engine/lua_random.h"
#include <cerrno>
#include <cstdlib>
#include <limits>
#include <random>
#include <string>
#include <vector>
#include "deepmind/lua/push.h"
#include "deepmind/lua/read.h"
namespace deepmind {
namespace lab {
namespace {
// The fundamental integer type of our pseudo-random bit generator,
// an unsigned 64(ish)-bit integer.
using RbgNumType = ::std::mt19937_64::result_type;
// A range-checking version of lua::Read that results in an error if the number
// on the Lua stack is too large to be represented by a 64-bit integer.
//
// Warning: this code is subtle. E.g. it has to be "x < " below, and not "x <=",
// and we must check for "true" comparisons, not false ones, in order to catch
// NaNs.
bool ReadLargeNumber(lua_State* L, int idx, RbgNumType* num) {
lua_Number x;
if (lua::Read(L, idx, &x) &&
std::numeric_limits<RbgNumType>::min() <= x &&
x < std::numeric_limits<RbgNumType>::max()) {
*num = x;
return true;
} else {
return false;
}
}
} // namespace
lua::NResultsOr LuaRandom::Require(lua_State* L) {
if (auto* prbg = static_cast<std::mt19937_64*>(
lua_touserdata(L, lua_upvalueindex(1)))) {
LuaRandom::CreateObject(L, prbg);
return 1;
} else {
return "Missing std::mt19937_64 pointer in up value!";
}
}
const char* LuaRandom::ClassName() {
return "deepmind.lab.RandomView";
}
void LuaRandom::Register(lua_State* L) {
const Class::Reg methods[] = {
{"discreteDistribution", &Class::Member<&LuaRandom::DiscreteDistribution>},
{"normalDistribution", &Class::Member<&LuaRandom::NormalDistribution>},
{"seed", &Class::Member<&LuaRandom::Seed>},
{"uniformInt", &Class::Member<&LuaRandom::UniformInt>},
{"uniformReal", &Class::Member<&LuaRandom::UniformReal>},
};
Class::Register(L, methods);
}
lua::NResultsOr LuaRandom::Seed(lua_State* L) {
std::string s;
RbgNumType k;
if (ReadLargeNumber(L, -1, &k)) {
prbg_->seed(k);
return 0;
} else if (lua::Read(L, -1, &s)) {
auto& err = errno; // cache TLS-lookup
char* ep;
err = 0;
unsigned long long int n = std::strtoull(s.data(), &ep, 0);
if (ep != s.data() && *ep == '\0' && err == 0 &&
n <= std::numeric_limits<RbgNumType>::max()) {
prbg_->seed(n);
return 0;
}
}
return "Argument is not a valid seed value.";
}
lua::NResultsOr LuaRandom::UniformInt(lua_State* L) {
RbgNumType a, b;
if (!ReadLargeNumber(L, -2, &a) ||
!ReadLargeNumber(L, -1, &b) ||
!(a <= b)) {
return "Arguments do not form a valid range.";
}
lua::Push(L, std::uniform_int_distribution<RbgNumType>(a, b)(*prbg_));
return 1;
}
lua::NResultsOr LuaRandom::UniformReal(lua_State* L) {
lua_Number a, b;
if (!lua::Read(L, -2, &a) || !lua::Read(L, -1, &b) || !(a <= b) ||
!(b - a <= std::numeric_limits<lua_Number>::max())) {
return "Arguments do not form a valid range.";
}
lua::Push(L, std::uniform_real_distribution<lua_Number>(a, b)(*prbg_));
return 1;
}
lua::NResultsOr LuaRandom::NormalDistribution(lua_State* L) {
lua_Number mean, stddev;
if (!lua::Read(L, -2, &mean) || !lua::Read(L, -1, &stddev)) {
return "Invalid arguments - 2 numbers expected..";
}
lua::Push(L, std::normal_distribution<lua_Number>(mean, stddev)(*prbg_));
return 1;
}
lua::NResultsOr LuaRandom::DiscreteDistribution(lua_State* L) {
std::vector<double> weights;
if (!lua::Read(L, -1, &weights) || weights.empty()) {
return "Invalid arguments - non empty list of numeric weights expected.";
}
lua::Push(L, std::discrete_distribution<lua_Integer>(
weights.begin(), weights.end())(*prbg_) + 1);
return 1;
}
} // namespace lab
} // namespace deepmind