-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.moon
59 lines (46 loc) · 868 Bytes
/
util.moon
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
return_v = false
value_v = 0
deepcopy = (org) ->
copy = {}
if "table" == type org
for k, v in next, org, nil
copy[deepcopy k] = deepcopy org
setmetatable copy, deepcopy getmetatable org
else
copy = org
copy
gauss_random = ->
if return_v
return_v = false
return value_v
u = 2 * math.random! - 1
v = 2 * math.random! - 1
r = u^2 + v^2
if r == 0 or r > 1
return gauss_random!
c = math.sqrt -2 * (math.log r) / r
value_v = v * c
u * c
randf = (a, b) ->
(b - a) * math.random! + a
randi = (a, b) ->
math.floor (b - a) * math.random! + a
randn = (mu, sigma) ->
mu + gauss_random! * sigma
cap = (n) ->
if n < 0
return 0
if n > 1
return 1
n
distance = (a, b) ->
math.sqrt (a[1] - b[1])^2 + (a[2] - b[2])^2
{
:deepcopy
:gauss_random
:randf
:randi
:randn
:cap
:distance
}