naev 0.12.6
nlua_rnd.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <lauxlib.h>
11#include <math.h>
13
14#include "nlua_rnd.h"
15
16#include "nluadef.h"
17#include "rng.h"
18
19/* Random methods. */
20static int rndL_int( lua_State *L );
21static int rndL_sigma( lua_State *L );
22static int rndL_twosigma( lua_State *L );
23static int rndL_threesigma( lua_State *L );
24static int rndL_uniform( lua_State *L );
25static int rndL_angle( lua_State *L );
26static int rndL_permutation( lua_State *L );
27
28static const luaL_Reg rnd_methods[] = { { "rnd", rndL_int },
29 { "sigma", rndL_sigma },
30 { "twosigma", rndL_twosigma },
31 { "threesigma", rndL_threesigma },
32 { "uniform", rndL_uniform },
33 { "angle", rndL_angle },
34 { "permutation", rndL_permutation },
35 { 0, 0 } };
36
43int nlua_loadRnd( nlua_env env )
44{
45 nlua_register( env, "rnd", rnd_methods, 0 );
46 return 0;
47}
48
84static int rndL_int( lua_State *L )
85{
86 int l, h;
87 int o = lua_gettop( L );
88
89 if ( o == 0 )
90 lua_pushnumber( L, RNGF() ); /* random double 0 <= x <= 1 */
91 else if ( o == 1 ) { /* random int 0 <= x <= parameter */
92 l = luaL_checkint( L, 1 );
93 lua_pushnumber( L, RNG( 0, l ) );
94 } else if ( o >= 2 ) { /* random int parameter 1 <= x <= parameter 2 */
95 l = luaL_checkint( L, 1 );
96 h = luaL_checkint( L, 2 );
97 lua_pushnumber( L, RNG( l, h ) );
98 } else
99 NLUA_INVALID_PARAMETER( L, 1 );
100
101 return 1; /* unless it's returned 0 already it'll always return a parameter
102 */
103}
104
117static int rndL_sigma( lua_State *L )
118{
119 lua_pushnumber( L, RNG_1SIGMA() );
120 return 1;
121}
122
136static int rndL_twosigma( lua_State *L )
137{
138 lua_pushnumber( L, RNG_2SIGMA() );
139 return 1;
140}
141
156static int rndL_threesigma( lua_State *L )
157{
158 lua_pushnumber( L, RNG_3SIGMA() );
159 return 1;
160}
161
175static int rndL_uniform( lua_State *L )
176{
177 int o = lua_gettop( L );
178
179 if ( o == 0 )
180 lua_pushnumber( L, RNGF() ); /* random double 0 <= x <= 1 */
181 else if ( o == 1 ) { /* random int 0 <= x <= parameter */
182 int l = luaL_checknumber( L, 1 );
183 lua_pushnumber( L, RNGF() * l );
184 } else if ( o >= 2 ) { /* random int parameter 1 <= x <= parameter 2 */
185 int l = luaL_checknumber( L, 1 );
186 int h = luaL_checknumber( L, 2 );
187 lua_pushnumber( L, l + ( h - l ) * RNGF() );
188 } else
189 NLUA_INVALID_PARAMETER( L, 1 );
190
191 return 1; /* unless it's returned 0 already it'll always return a parameter
192 */
193}
194
202static int rndL_angle( lua_State *L )
203{
204 lua_pushnumber( L, RNGF() * 2. * M_PI );
205 return 1;
206}
207
222static int rndL_permutation( lua_State *L )
223{
224 int *values;
225 int max;
226 int new_table;
227
228 if ( lua_isnumber( L, 1 ) ) {
229 max = lua_tointeger( L, 1 );
230 new_table = 1;
231 } else if ( lua_istable( L, 1 ) ) {
232 max = (int)lua_objlen( L, 1 );
233 new_table = 0;
234 } else
235 NLUA_INVALID_PARAMETER( L, 1 );
236
237 /* Create the list. */
238 values = malloc( sizeof( int ) * max );
239 for ( int i = 0; i < max; i++ )
240 values[i] = i;
241
242 /* Fisher-Yates shuffling algorithm */
243 for ( int i = max - 1; i >= 0; --i ) {
244 /* Generate a random number in the range [0, max-1] */
245 int j = randint() % ( i + 1 );
246
247 /* Swap the last element with an element at a random index. */
248 int temp = values[i];
249 values[i] = values[j];
250 values[j] = temp;
251 }
252
253 /* Now either return a new table or permute the given table. */
254 lua_newtable( L );
255 for ( int i = 0; i < max; i++ ) {
256 lua_pushnumber( L, values[i] + 1 );
257 if ( !new_table )
258 lua_gettable( L, 1 );
259 lua_rawseti( L, -2, i + 1 );
260 }
261
262 free( values );
263 return 1;
264}
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
int nlua_loadRnd(nlua_env env)
Loads the Random Number Lua library.
Definition nlua_rnd.c:43
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
static const luaL_Reg rnd_methods[]
Definition nlua_rnd.c:28
unsigned int randint(void)
Gets a random integer.
Definition rng.c:165