-
Notifications
You must be signed in to change notification settings - Fork 0
/
dice.lua
114 lines (80 loc) · 1.61 KB
/
dice.lua
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env lua
function main (argv)
local rolls, constant = parse(argv)
local min = 0
local max = 0
local combinations = 1;
for d,n in pairs(rolls) do
min = min + n
max = max + n * d
combinations = combinations * d^n
end
local polys = mkpolys(rolls)
local biggun = 1
for i, x in pairs(polys) do
biggun = polymul(biggun, x)
end
print "";
for x = min, max+1 do
local coeff = biggun[x]
if coeff then
local px = coeff / combinations
local hg = histogram(px)
print( string.format("%d\t\t%.5f\t\t%s", x + constant, px, hg) )
end
end
print "";
end
function parse (args)
local pat = "(%d+)d(%d+)"
local rv = {}
local adder = 0;
for i, xi in ipairs(arg) do
if (string.find(xi, pat)) then
_, _, n, d = string.find(xi, pat)
rv[d] = rv[d] or 0
rv[d] = rv[d] + n
else
adder = adder + xi
end
end
return rv, adder
end
function mkpolys (rolls)
local polys = {}
for d, n in pairs(rolls) do
for i = 1, n do
local p = {}
for j = 1, d do
p[j] = 1
end
table.insert(polys, p)
end
end
return polys
end
function polymul (x, y)
local rv = {};
if (x == 1) then
return y
end
for i, xi in pairs(x) do
for j, yj in pairs(y) do
local cell = i + j
if rv[cell] == nil then
rv[cell] = 0;
end
rv[cell] = rv[cell] + (xi * yj)
end
end
return rv
end
function histogram (n)
local num = (0.5 + (500 * n));
local tmp = {}
for i = 0, num do
tmp[i] = "#"
end
return table.concat(tmp, "")
end
main(arg)