naev 0.11.5
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
12#include "naev.h"
15#include "nlua_rnd.h"
16
17#include "log.h"
18#include "map.h"
19#include "ndata.h"
20#include "nluadef.h"
21#include "rng.h"
22
23/* Random methods. */
24static int rndL_int( lua_State *L );
25static int rndL_sigma( lua_State *L );
26static int rndL_twosigma( lua_State *L );
27static int rndL_threesigma( lua_State *L );
28static int rndL_uniform( lua_State *L );
29static int rndL_angle( lua_State *L );
30static int rndL_permutation( lua_State *L );
31static const luaL_Reg rnd_methods[] = {
32 { "rnd", rndL_int },
33 { "sigma", rndL_sigma },
34 { "twosigma", rndL_twosigma },
35 { "threesigma", rndL_threesigma },
36 { "uniform", rndL_uniform },
37 { "angle", rndL_angle },
38 { "permutation", rndL_permutation },
39 {0,0}
40};
48int nlua_loadRnd( nlua_env env )
49{
50 nlua_register(env, "rnd", rnd_methods, 0);
51 return 0;
52}
53
88static int rndL_int( lua_State *L )
89{
90 int l,h;
91 int o = lua_gettop(L);
92
93 if (o==0)
94 lua_pushnumber(L, RNGF() ); /* random double 0 <= x <= 1 */
95 else if (o==1) { /* random int 0 <= x <= parameter */
96 l = luaL_checkint(L,1);
97 lua_pushnumber(L, RNG(0, l));
98 }
99 else if (o>=2) { /* random int parameter 1 <= x <= parameter 2 */
100 l = luaL_checkint(L,1);
101 h = luaL_checkint(L,2);
102 lua_pushnumber(L, RNG(l,h));
103 }
104 else
105 NLUA_INVALID_PARAMETER(L,1);
106
107 return 1; /* unless it's returned 0 already it'll always return a parameter */
108}
120static int rndL_sigma( lua_State *L )
121{
122 lua_pushnumber(L, RNG_1SIGMA());
123 return 1;
124}
138static int rndL_twosigma( lua_State *L )
139{
140 lua_pushnumber(L, RNG_2SIGMA());
141 return 1;
142}
157static int rndL_threesigma( lua_State *L )
158{
159 lua_pushnumber(L, RNG_3SIGMA());
160 return 1;
161}
162
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 }
185 else if (o>=2) { /* random int parameter 1 <= x <= parameter 2 */
186 int l = luaL_checknumber( L, 1 );
187 int h = luaL_checknumber( L, 2 );
188 lua_pushnumber( L, l + (h-l) * RNGF() );
189 }
190 else
191 NLUA_INVALID_PARAMETER(L,1);
192
193 return 1; /* unless it's returned 0 already it'll always return a parameter */
194}
195
203static int rndL_angle( lua_State *L )
204{
205 lua_pushnumber( L, RNGF() * 2. * M_PI );
206 return 1;
207}
208
223static int rndL_permutation( lua_State *L )
224{
225 int *values;
226 int max;
227 int new_table;
228
229 if (lua_isnumber(L,1)) {
230 max = lua_tointeger(L,1);
231 new_table = 1;
232 }
233 else if (lua_istable(L,1)) {
234 max = (int) lua_objlen(L,1);
235 new_table = 0;
236 }
237 else
238 NLUA_INVALID_PARAMETER(L,1);
239
240 /* Create the list. */
241 values = malloc( sizeof(int)*max );
242 for (int i=0; i<max; i++)
243 values[i]=i;
244
245 /* Fisher-Yates shuffling algorithm */
246 for (int i = max-1; i >= 0; --i){
247 /* Generate a random number in the range [0, max-1] */
248 int j = randint() % (i+1);
249
250 /* Swap the last element with an element at a random index. */
251 int temp = values[i];
252 values[i] = values[j];
253 values[j] = temp;
254 }
255
256 /* Now either return a new table or permute the given table. */
257 lua_newtable(L);
258 for (int i=0; i<max; i++) {
259 lua_pushnumber( L, values[i]+1 );
260 if (!new_table)
261 lua_gettable( L, 1 );
262 lua_rawseti( L, -2, i+1 );
263 }
264
265 free( values );
266 return 1;
267}
Header file with generic functions and naev-specifics.
static int rndL_twosigma(lua_State *L)
Creates a number in the two-sigma range [-2:2].
Definition nlua_rnd.c:138
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:203
static int rndL_sigma(lua_State *L)
Creates a number in the one-sigma range [-1:1].
Definition nlua_rnd.c:120
static int rndL_threesigma(lua_State *L)
Creates a number in the three-sigma range [-3:3].
Definition nlua_rnd.c:157
int nlua_loadRnd(nlua_env env)
Loads the Random Number Lua library.
Definition nlua_rnd.c:48
static int rndL_permutation(lua_State *L)
Creates a random permutation.
Definition nlua_rnd.c:223
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:88
static const luaL_Reg rnd_methods[]
Definition nlua_rnd.c:31
unsigned int randint(void)
Gets a random integer.
Definition rng.c:167