Skip to content

Commit 2245595

Browse files
carlosmpsilvacarlosmpsilva
authored andcommitted
Some fixes in seed method
1 parent 38dab67 commit 2245595

File tree

3 files changed

+49
-36
lines changed

3 files changed

+49
-36
lines changed

main/main.script

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,46 @@ local function test_pcg32_double_numbers()
3535
assert_double(next(), 0.74860336142592)
3636
end
3737

38+
39+
local function check_pcg32_42_54_seed(pcg)
40+
41+
local function next()
42+
return pcg:range(1, 100)
43+
end
44+
45+
assert(next() == 84)
46+
assert(next() == 98)
47+
assert(next() == 25)
48+
assert(next() == 56)
49+
assert(next() == 56)
50+
end
51+
3852
local function test_pcg32_ranges()
3953

4054
local pcg = rng.pcg32(42, 54)
4155

42-
local function next(min, max)
43-
return pcg:range(min, max)
44-
end
56+
check_pcg32_42_54_seed(pcg)
4557

46-
assert(next(1, 100) == 84)
47-
assert(next(1, 100) == 98)
48-
assert(next(1, 100) == 25)
49-
assert(next(1, 100) == 56)
50-
assert(next(1, 100) == 56)
51-
52-
assert(next(50, 50) == 50)
53-
assert(not pcall(function() next(100, 1) end))
58+
assert(pcg:range(50, 50) == 50)
59+
assert(not pcall(function() pcg:range(100, 1) end))
5460
end
5561

5662
local function test_pcg32_double_ranges()
5763

5864
local pcg = rng.pcg32(42, 54)
5965

60-
local function next(min, max)
61-
return pcg:double_range(min, max)
66+
local function next()
67+
return pcg:double_range(2.5, 7.5)
6268
end
6369

64-
assert_double(next(2.5, 7.5), 5.151551102789)
65-
assert_double(next(2.5, 7.5), 4.4078333488218)
66-
assert_double(next(2.5, 7.5), 5.6350402803242)
67-
assert_double(next(2.5, 7.5), 4.5746877718658)
68-
assert_double(next(2.5, 7.5), 5.7430168080011)
70+
assert_double(next(), 5.151551102789)
71+
assert_double(next(), 4.4078333488218)
72+
assert_double(next(), 5.6350402803242)
73+
assert_double(next(), 4.5746877718658)
74+
assert_double(next(), 5.7430168080011)
6975

70-
assert_double(next(5.5, 5.5), 5.5)
71-
assert(not pcall(function() next(7.5, 2.5) end))
76+
assert_double(pcg:double_range(5.5, 5.5), 5.5)
77+
assert(not pcall(function() pcg:double_range(7.5, 2.5) end))
7278
end
7379

7480
local function test_pcg32_roll()
@@ -104,8 +110,13 @@ local function test_pcg32_seed()
104110
local pcg2 = rng.pcg32(2, 2)
105111
local pcg3 = rng.pcg32(2, 2)
106112

107-
-- checks a pcg32 can be created without specifying seed
113+
-- verifies if a pcg32 can be created without specifying seed
108114
pcg1:number()
115+
116+
-- verifies if a pcg32 can be reseeded
117+
pcg1:seed(42, 54)
118+
check_pcg32_42_54_seed(pcg1)
119+
109120
-- checks 2 pcg32 instances with the same seed produce the same results
110121
assert(pcg2:number() == pcg3:number())
111122
assert(pcg2:number() == pcg3:number())

rng/include/pcg.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <math.h>
4+
#include <stdio.h>
45

56
class Pcg32
67
{
@@ -12,8 +13,8 @@ class Pcg32
1213

1314
~Pcg32() { Release(); }
1415

15-
void set_seed(uint64_t stat, uint64_t seq) {
16-
pcg32_srandom_r(&state, stat, seq);
16+
void set_seed(uint64_t stat, uint64_t inc) {
17+
pcg32_srandom_r(&state, stat, inc);
1718
}
1819

1920
uint32_t number() {

rng/src/pcg.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ class LuaPcg32 {
4545

4646
static int seed(lua_State *l) {
4747
Pcg32 *o = checkInstance(l, 1);
48-
int state = luaL_optinteger(l, 2, 0);
49-
int seq = 0;
48+
long state = luaL_optinteger(l, 2, 0);
49+
long inc = 0;
5050

5151
if (state == 0) {
5252
uint64_t* seeds = new_seed();
5353
state = seeds[0];
54-
seq = seeds[1];
54+
inc = seeds[1];
5555
} else {
56-
seq = luaL_optinteger(l, 2, 0);
56+
inc = luaL_optinteger(l, 3, 0);
5757
}
58-
59-
o->set_seed(state, seq);
58+
59+
o->set_seed(state, inc);
6060
return 0;
6161
}
6262

@@ -128,16 +128,18 @@ class LuaPcg32 {
128128
return 1;
129129
}
130130

131-
static int gc_account(lua_State *L) {
131+
static int gc_pcg(lua_State *L) {
132132
Pcg32 *o = (Pcg32*)lua_unboxpointer(L, 1);
133133
delete o;
134134
return 0;
135135
}
136136

137137
public:
138138
static void Register(lua_State* L) {
139-
lua_newtable(L); int methodtable = lua_gettop(L);
140-
luaL_newmetatable(L, className); int metatable = lua_gettop(L);
139+
lua_newtable(L);
140+
int methodtable = lua_gettop(L);
141+
luaL_newmetatable(L, className);
142+
int metatable = lua_gettop(L);
141143

142144
lua_pushliteral(L, "__metatable");
143145
lua_pushvalue(L, methodtable);
@@ -148,7 +150,7 @@ class LuaPcg32 {
148150
lua_settable(L, metatable);
149151

150152
lua_pushliteral(L, "__gc");
151-
lua_pushcfunction(L, gc_account);
153+
lua_pushcfunction(L, gc_pcg);
152154
lua_settable(L, metatable);
153155

154156
lua_pop(L, 1); // drop metatable
@@ -166,8 +168,8 @@ class LuaPcg32 {
166168

167169
if (arg_count > 0) {
168170
long stat = luaL_checknumber(L, 1);
169-
long seq = luaL_checknumber(L, 2);
170-
o->set_seed(stat, seq);
171+
long inc = luaL_checknumber(L, 2);
172+
o->set_seed(stat, inc);
171173
}
172174

173175
lua_boxpointer(L, o);
@@ -200,7 +202,6 @@ const luaL_reg LuaPcg32::methods[] = {
200202
method(LuaPcg32, seed),
201203
method(LuaPcg32, number),
202204
{ "double", LuaPcg32::double_num },
203-
//method(LuaPcg32, double_num),
204205
method(LuaPcg32, range),
205206
method(LuaPcg32, double_range),
206207
method(LuaPcg32, roll),

0 commit comments

Comments
 (0)