View difference between Paste ID: Sid2VzNc and 32UPwvW6
SHOW: | | - or go back to the newest paste.
1-
local sqrt, log, random, pi = math.sqrt, math.log, math.random, math.pi
1+
local sqrt, log, random, pi, floor = math.sqrt, math.log, math.random, math.pi, math.floor
2
3
local function gauss(mi, sigmaSquared)
4
	local x1, x2, w, y1, y2
5
	repeat
6
		x1 = 2.0 * random() - 1.0
7
		x2 = 2.0 * random() - 1.0
8
		w = x1 * x1 + x2 * x2
9
	until w <= 1.0
10
11
	w = sqrt( (-2.0 * log( w ) ) / w )
12
	y1 = x1 * w
13
	y2 = x2 * w
14
	return y1*sigmaSquared+mi, y2*sigmaSquared+mi
15
end
16
17-
-- returns 2 random values from range <0, 1>
17+
local function dice(N, sides)
18-
local y1, y2 = gauss(0.5, 1/(2*pi))
18+
	local mi, sigmaSquared = N*(sides-1)/2 + 1, (sides-1)*N/(2*pi)
19-
print(y1, y2)
19+
	local y1,y2 = gauss(mi, sigmaSquared)
20
	y1,y2 = floor(y1+0.5), floor(y2+0.5)
21
	y1 = (y1<N) and N or ((y1>sides*N) and sides*N or y1)
22
	y2 = (y2<N) and N or ((y2>sides*N) and sides*N or y2)
23
	return y1,y2
24
end