Skip to content

Commit

Permalink
rnd.range was causing a crash when MIN is bigger than MAX. Error mess…
Browse files Browse the repository at this point in the history
…age added.
  • Loading branch information
selimanac committed Aug 10, 2020
1 parent d591df6 commit 6213cc4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ You can use PCG Random in your own project by adding this project as a [Defold l

## Usage

### rnd.seed(`init_state, init_seq`)
### rnd.seed(`init_state`, `init_seq`)

Seeds the random number generator.
Random number generator is always initialized by using entropy seed. You don't need to call this method unless you want to control the seed.
Expand All @@ -35,7 +35,7 @@ Random number generator is always initialized by using entropy seed. You don’t

Returns a 32 bit unsigned integer.

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

Returns a 32 bit unsigned integer between min and max values. Only for positive numbers(unsigned integers).
Same as **math.random(3,20)**
Expand All @@ -62,6 +62,10 @@ Testing entropy.

## Release Notes

1.2.2

- `rnd.range` was causing a crash when MIN is bigger than MAX. Error message added.

1.2.1

- Added static seed
Expand Down
3 changes: 3 additions & 0 deletions main/main.script
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ local function test()
print("Result (unsigned int): " .. num)
end

print("Error check:")
local z = rnd.range(1, 0)
print(z)
print("--------------------")
end

Expand Down
43 changes: 25 additions & 18 deletions pcgrandom/src/pcgrandom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ static int range(lua_State *L)
int top = lua_gettop(L);
min = luaL_checknumber(L, 1);
max = luaL_checknumber(L, 2);


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

max++;
num = pcg32_boundedrand_r(&rng, (max - min)) + min;
lua_pushnumber(L, num);
Expand Down Expand Up @@ -112,12 +119,12 @@ static int check(lua_State *L)
printf("\n");

printf("pcg32_random_r:\n"
" - result: 32-bit unsigned int (uint32_t)\n"
" - period: 2^64 (* 2^63 streams)\n"
" - state type: pcg32_random_t (%zu bytes)\n"
" - output func: XSH-RR\n"
"\n",
sizeof(pcg32_random_t));
" - result: 32-bit unsigned int (uint32_t)\n"
" - period: 2^64 (* 2^63 streams)\n"
" - state type: pcg32_random_t (%zu bytes)\n"
" - output func: XSH-RR\n"
"\n",
sizeof(pcg32_random_t));

for (round = 1; round <= rounds; ++round)
{
Expand Down Expand Up @@ -163,9 +170,9 @@ static int check(lua_State *L)
}

printf(" Cards:");
static const char number[] = {'A', '2', '3', '4', '5', '6', '7',
'8', '9', 'T', 'J', 'Q', 'K'};
static const char suit[] = {'h', 'c', 'd', 's'};
static const char number[] ={ 'A', '2', '3', '4', '5', '6', '7',
'8', '9', 'T', 'J', 'Q', 'K' };
static const char suit[] ={ 'h', 'c', 'd', 's' };
for (i = 0; i < CARDS; ++i)
{
printf(" %c%c", number[cards[i] / SUITS], suit[cards[i] % SUITS]);
Expand All @@ -179,16 +186,16 @@ static int check(lua_State *L)
}

static const luaL_reg Module_methods[] =
{
{

{"seed", seedgen},
{"double", double_num},
{"roll", roll},
{"toss", toss},
{"range", range},
{"number", number},
{"check", check},
{0, 0}};
{ "seed", seedgen },
{ "double", double_num },
{ "roll", roll },
{ "toss", toss },
{ "range", range },
{ "number", number },
{ "check", check },
{ 0, 0 } };

static void LuaInit(lua_State *L)
{
Expand Down

0 comments on commit 6213cc4

Please sign in to comment.