lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Interesting approach, as math.random(), based on ansi C `rand()`, isn’t really considered as a good random generator.

Personally, I did something analog, but as a change to the Lua source code, by defining a `luai_rand` macro in `luaconf.h`:

#if defined(LUA_USE_BSD)
#define luai_rand arc4random
#else
#define luai_rand rand
#endif

where `LUA_USE_BSD` is defined for Mac OS X (LUA_USE_MACOSX) ans iOS (LUA_USE_IOS).

And in `lmathlib.c`, `rand` is simply replaced by `luai_rand:

static int math_random (lua_State *L) {
  /* …  */
  lua_Number r = (lua_Number)(luai_rand()%RAND_MAX) / (lua_Number)RAND_MAX;
 /*…*/
}

This works great as well…

Jean-Luc

> Le 21 janv. 2015 à 16:18, Michael Savage <mikejsavage@gmail.com> wrote :
> 
> lua-arc4random is a wrapper around OpenBSD's arc4random. It provides two
> methods, including a safe drop-in replacement for math.random.
> 
> arc4random implements ChaCha20, which is a strong and efficient RNG,
> suitable for generating cryptographic secrets. It is also more robust
> than other methods like urandom, which fails when you run out of file
> descriptors, and has issues with chroots. You can find more information
> in the man file:
> 
> http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/arc4random.3
> 
> Under the hood, I am using code from libressl-portable to provide the
> arc4random calls on operating systems that do not support it.
> 
> 
> Mike
>