forked from svarog2741/xrluafix
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLibMath.cpp
78 lines (71 loc) · 1.61 KB
/
LibMath.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "pch.hpp"
#include "LibMath.h"
#include <random>
static std::random_device ndrng;
static std::mt19937 intgen;
static std::uniform_real_distribution<float> float_random;
int gen_random_in_range(int a1, int a2)
{
//unsigned?
std::uniform_int_distribution dist(a1, a2);
return dist(intgen);
}
static int math_randomseed(lua_State* L)
{
switch (lua_gettop(L))
{
case 0:
{
intgen.seed(ndrng());
break;
}
case 1:
{
const int seed_value = luaL_checkint(L, 1);
intgen.seed(seed_value);
break;
}
default: return luaL_error(L, "math_randomseed: wrong number of arguments");
}
return 0;
}
static int math_random(lua_State* L)
{
switch (lua_gettop(L))
{
case 0:
{
lua_pushnumber(L, (lua_Number)float_random(intgen));
break;
}
case 1:
{
const int u = luaL_checkint(L, 1);
luaL_argcheck(L, 1<=u, 1, "interval is empty");
lua_pushinteger(L, gen_random_in_range(1, u));
break;
}
case 2:
{
const int l = luaL_checkint(L, 1);
const int u = luaL_checkint(L, 2);
luaL_argcheck(L, l<=u, 2, "interval is empty");
lua_pushinteger(L, gen_random_in_range(l, u));
break;
}
default:
return luaL_error(L, "wrong number of arguments");
}
return 1;
}
int open_math(lua_State* L)
{
constexpr luaL_Reg mathlib[] =
{
{ "random", math_random },
{ "randomseed", math_randomseed },
{ nullptr, nullptr }
};
luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0);
return 0;
}