Skip to content

Commit

Permalink
rnd.double_range(min, max) added
Browse files Browse the repository at this point in the history
  • Loading branch information
selimanac committed Mar 21, 2021
1 parent 065f567 commit 2822b61
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ Same as **math.random(3,20)**
Returns a floating point between 0-1.
Same as **math.random()**

### rnd.double_range(`min`, `max`)

Returns a floating point between min - max.
Not fast as `rnd.double()`

### rnd.toss()

Toss a coin. Returns 0 or 1 (0 = 'H', 1 = 'T')
Expand All @@ -60,7 +65,11 @@ Testing entropy.



## Release Notes
## Release Notes

1.2.4

- `rnd.double_range(min, max)` added.

1.2.3

Expand Down
8 changes: 8 additions & 0 deletions main/main.script
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ local function test()
print("Double: " .. d) -- range [0,1]
end

print("--------------------")
print("DOUBLE RANGE 1.2 -> 2.5 ")
for i = 1, 10 do
d = rnd.double_range(1.2, 2.5)
print("Double Range: " .. d)
end

print("--------------------")
print("RANGE")
for i = 1, 10 do
Expand All @@ -55,6 +62,7 @@ local function test()
end

function init(self)

-- entropy test
test()

Expand Down
40 changes: 35 additions & 5 deletions pcgrandom/src/pcgrandom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ static uint64_t seedinitseq;
static uint64_t seeds[2];
static double d;

static double dmin;
static double dmax;

static void entropy_seed()
{
// Entropy seed with Time based fallback:
Expand All @@ -35,10 +38,36 @@ static void entropy_seed()
//pcg32_srandom_r(&rng, time(NULL) ^ (intptr_t)&printf, (intptr_t)&rng);
}

static int double_range(lua_State *L)
{
int top = lua_gettop(L);
dmin = luaL_checknumber(L, 1);
dmax = luaL_checknumber(L, 2);

if (dmin == dmax)
{
lua_pushnumber(L, dmin);
return 1;
}

if (dmin > dmax)
{
dmLogError("rnd.range: MAX(%i) must be bigger than MIN(%i)", dmax, dmin);
return 0;
}

num = pcg32_random_r(&rng);
d = (double)(num) / ((double)UINT32_MAX) * (dmax - dmin) + dmin;

lua_pushnumber(L, d);
assert(top + 1 == lua_gettop(L));
return 1;
}
static int double_num(lua_State *L)
{
int top = lua_gettop(L);
d = ldexp(pcg32_random_r(&rng), -32);

lua_pushnumber(L, d);
assert(top + 1 == lua_gettop(L));
return 1;
Expand All @@ -65,12 +94,12 @@ static int toss(lua_State *L)
static int range(lua_State *L)
{
int top = lua_gettop(L);
min = luaL_checknumber(L, 1);
max = luaL_checknumber(L, 2);
min = luaL_checkinteger(L, 1);
max = luaL_checkinteger(L, 2);

if (min == max)
{
lua_pushnumber(L, min);
lua_pushinteger(L, min);
return 1;
}

Expand All @@ -82,7 +111,7 @@ static int range(lua_State *L)

max++;
num = pcg32_boundedrand_r(&rng, (max - min)) + min;
lua_pushnumber(L, num);
lua_pushinteger(L, num);
assert(top + 1 == lua_gettop(L));
return 1;
}
Expand Down Expand Up @@ -110,7 +139,7 @@ static int number(lua_State *L)
{
int top = lua_gettop(L);
num = pcg32_random_r(&rng);
lua_pushnumber(L, num);
lua_pushinteger(L, num);
assert(top + 1 == lua_gettop(L));
return 1;
}
Expand Down Expand Up @@ -196,6 +225,7 @@ static const luaL_reg Module_methods[] =

{"seed", seedgen},
{"double", double_num},
{"double_range", double_range},
{"roll", roll},
{"toss", toss},
{"range", range},
Expand Down

0 comments on commit 2822b61

Please sign in to comment.