Skip to content

Commit

Permalink
Fixed [rnd.range(1,1) fails #3]
Browse files Browse the repository at this point in the history
  • Loading branch information
selimanac committed Feb 16, 2021
1 parent 6213cc4 commit c44ede4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ Testing entropy.

## Release Notes

1.2.3

- `rnd.range` returns min if min == max.

1.2.2

- `rnd.range` was causing a crash when MIN is bigger than MAX. Error message added.
Expand Down
6 changes: 5 additions & 1 deletion main/main.script
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ local function test()
end

print("Error check:")
local z = rnd.range(1, 0)
local z = rnd.range(10, 0)
print(z)
print("--------------------")
print("Return min:")
local z = rnd.range(5, 5)
print(z)
print("--------------------")
end
Expand Down
1 change: 1 addition & 0 deletions pcgrandom/include/entropy.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define ENTROPY_H_INCLUDED 1

#include <stdbool.h>
#include <stddef.h>

#if __cplusplus
extern "C" {
Expand Down
50 changes: 28 additions & 22 deletions pcgrandom/src/pcgrandom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#define LIB_NAME "pcgrandom"
#define MODULE_NAME "rnd"

#include <dmsdk/sdk.h>
#include <dmsdk/dlib/log.h>
#include "entropy.h"
#include "pcg_basic.h"
#include <dmsdk/dlib/log.h>
#include <dmsdk/sdk.h>
#include <math.h>
#include "entropy.h"

//#define __STDC_FORMAT_MACROS
//#include <inttypes.h>
Expand Down Expand Up @@ -68,8 +68,14 @@ static int range(lua_State *L)
min = luaL_checknumber(L, 1);
max = luaL_checknumber(L, 2);

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

if (min >= max) {
if (min > max)
{
dmLogError("rnd.range: MAX(%i) must be bigger than MIN(%i)", max, min);
return 0;
}
Expand Down Expand Up @@ -119,12 +125,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 @@ -170,9 +176,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 @@ -186,16 +192,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 c44ede4

Please sign in to comment.