naev 0.12.5
nlua_rnd.c File Reference

Lua bindings for the Naev random number generator. More...

#include "nlua_rnd.h"
#include "nluadef.h"
#include "rng.h"
Include dependency graph for nlua_rnd.c:

Go to the source code of this file.

Functions

static int rndL_int (lua_State *L)
 Bindings for interacting with the random number generator.
static int rndL_sigma (lua_State *L)
 Creates a number in the one-sigma range [-1:1].
static int rndL_twosigma (lua_State *L)
 Creates a number in the two-sigma range [-2:2].
static int rndL_threesigma (lua_State *L)
 Creates a number in the three-sigma range [-3:3].
static int rndL_uniform (lua_State *L)
 Gets a random number in the given range, with a uniform distribution.
static int rndL_angle (lua_State *L)
 Gets a random angle, i.e., a random number from 0 to 2*pi.
static int rndL_permutation (lua_State *L)
 Creates a random permutation.
int nlua_loadRnd (nlua_env env)
 Loads the Random Number Lua library.

Variables

static const luaL_Reg rnd_methods []

Detailed Description

Lua bindings for the Naev random number generator.

Definition in file nlua_rnd.c.

Function Documentation

◆ nlua_loadRnd()

int nlua_loadRnd ( nlua_env env)

Loads the Random Number Lua library.

Parameters
envLua environment.
Returns
0 on success.

Definition at line 43 of file nlua_rnd.c.

◆ rndL_angle()

int rndL_angle ( lua_State * L)
static

Gets a random angle, i.e., a random number from 0 to 2*pi.

Lua usage parameter: vec2.newP(radius, rnd.angle()) Lua return parameter: number A randomly generated angle, in radians.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: angle

Definition at line 202 of file nlua_rnd.c.

◆ rndL_int()

int rndL_int ( lua_State * L)
static

Bindings for interacting with the random number generator.

This module not only allows basic random number generation, but it also handles more complicated statistical stuff.

Example usage would be:

if rnd.rnd() < 0.5 then
-- 50% chance of this happening
else
-- And 50% chance of this happening
end

Lua module: rnd

Gets a random number. With no parameters it returns a random float between 0 and 1.

With one parameter it returns a whole number between 0 and that number (both included). With two parameters it returns a whole number between both parameters (both included).

Lua usage parameter: n = rnd() – Number in range [0:1]. Lua usage parameter: n = rnd(5) – Number in range [0:5]. Lua usage parameter: n = rnd(3,5) – Number in range [3,5].

Lua function parameter: number x First parameter, read description for details. Lua function parameter: number y Second parameter, read description for details. Lua return parameter: number A randomly generated number, read description for details.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: rnd

Definition at line 84 of file nlua_rnd.c.

◆ rndL_permutation()

int rndL_permutation ( lua_State * L)
static

Creates a random permutation.

This creates a list from 1 to input and then randomly permutates it, however, if an ordered table is passed as a parameter, that is randomly permuted instead.

Lua usage parameter: t = rnd.permutation( 5 ) Lua usage parameter: t = rnd.permutation( {"cat", "dog", "cheese"} )

Lua function parameter: number|table input Maximum value to permutate to. Lua return parameter: table A randomly permutated table.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: permutation

Definition at line 222 of file nlua_rnd.c.

◆ rndL_sigma()

int rndL_sigma ( lua_State * L)
static

Creates a number in the one-sigma range [-1:1].

A one sigma range means that it creates a number following the normal distribution but limited to the 63% quadrant. This means that the number is biased towards 0, but can become either 1 or -1. It's a fancier way of generating random numbers.

Lua usage parameter: n = 5.5 + rnd.sigma()/2. – Creates a number from 5 to 6 slightly biased to 5.5. Lua return parameter: number A number from [-1:1] biased slightly towards 0.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: sigma

Definition at line 117 of file nlua_rnd.c.

◆ rndL_threesigma()

int rndL_threesigma ( lua_State * L)
static

Creates a number in the three-sigma range [-3:3].

This function behaves much like its brothers rnd.sigma and rnd.twosigma. The main difference is that it uses the three-sigma range which is the 99% quadrant. It will rarely generate numbers outside the [-2:2] range (about 5% of the time) and create numbers outside of the [-1:1] range about 37% of the time. This can be used when you want extremes to appear rarely.

Lua usage parameter: n = 5.5 + rnd.threesigma()/6. – Creates a number from 5 to 6 totally biased to 5.5.

Lua return parameter: number A number from [-3:3] biased totally towards 0.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: threesigma

Definition at line 156 of file nlua_rnd.c.

◆ rndL_twosigma()

int rndL_twosigma ( lua_State * L)
static

Creates a number in the two-sigma range [-2:2].

This function behaves much like the rnd.sigma function but uses the two-sigma range, meaning that numbers are in the 95% quadrant and thus are much more random. They are biased towards 0 and approximately 63% will be within [-1:1]. The rest will be in either the [-2:-1] range or the [1:2] range.

Lua usage parameter: n = 5.5 + rnd.twosigma()/4. – Creates a number from 5 to 6 heavily biased to 5.5.

Lua return parameter: number A number from [-2:2] biased heavily towards 0.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: twosigma

Definition at line 136 of file nlua_rnd.c.

◆ rndL_uniform()

int rndL_uniform ( lua_State * L)
static

Gets a random number in the given range, with a uniform distribution.

Lua usage parameter: n = uniform() – Real number in the interval [0,1]. Lua usage parameter: n = uniform(5) – Real number in the interval [0,5]. Lua usage parameter: n = uniform(3,5) – Real number in the interval [3,5].

Lua function parameter: number x First parameter, read description for details. Lua function parameter: number y Second parameter, read description for details. Lua return parameter: number A randomly generated number, read description for details.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: uniform

Definition at line 175 of file nlua_rnd.c.

Variable Documentation

◆ rnd_methods

const luaL_Reg rnd_methods[]
static
Initial value:
= { { "rnd", rndL_int },
{ "sigma", rndL_sigma },
{ "twosigma", rndL_twosigma },
{ "threesigma", rndL_threesigma },
{ "uniform", rndL_uniform },
{ "angle", rndL_angle },
{ "permutation", rndL_permutation },
{ 0, 0 } }
static int rndL_twosigma(lua_State *L)
Creates a number in the two-sigma range [-2:2].
Definition nlua_rnd.c:136
static int rndL_angle(lua_State *L)
Gets a random angle, i.e., a random number from 0 to 2*pi.
Definition nlua_rnd.c:202
static int rndL_sigma(lua_State *L)
Creates a number in the one-sigma range [-1:1].
Definition nlua_rnd.c:117
static int rndL_threesigma(lua_State *L)
Creates a number in the three-sigma range [-3:3].
Definition nlua_rnd.c:156
static int rndL_permutation(lua_State *L)
Creates a random permutation.
Definition nlua_rnd.c:222
static int rndL_uniform(lua_State *L)
Gets a random number in the given range, with a uniform distribution.
Definition nlua_rnd.c:175
static int rndL_int(lua_State *L)
Bindings for interacting with the random number generator.
Definition nlua_rnd.c:84

Random Lua methods.

Definition at line 28 of file nlua_rnd.c.