fork download
  1.  
  2. math.randomseed(os.time())
  3.  
  4. local result_pos_t = {}
  5. local function result_pos(num, max)
  6. local k = num .. ',' .. max
  7. if not result_pos_t[k] then
  8. if num == 0 then
  9. result_pos_t[k] = (max - num) + 1
  10. else
  11. result_pos_t[k] = result_pos(num - 1, max) + (max - num) + 1
  12. end
  13. end
  14. return result_pos_t[k]
  15. end
  16.  
  17. local function crazy_dice(n)
  18. local size = result_pos(n, n)
  19. local rand = math.random(size)
  20. for i = 0, n do
  21. if rand <= result_pos(i, n) then
  22. return i
  23. end
  24. end
  25. error()
  26. end
  27.  
  28. local tally = {}
  29. local count = 100000
  30. local sides = 30
  31. for i = 1, count do
  32. local res = crazy_dice(sides)
  33. if not tally[res] then tally[res] = 1
  34. else tally[res] = tally[res] + 1
  35. end
  36. end
  37.  
  38. for i = 0, sides do
  39. print(i, (tally[i] or 1) / count)
  40. end
Success #stdin #stdout 2.27s 2540KB
stdin
Standard input is empty
stdout
0	0.06177
1	0.06028
2	0.05962
3	0.05475
4	0.05426
5	0.05273
6	0.04982
7	0.05005
8	0.04655
9	0.04514
10	0.04299
11	0.04068
12	0.03976
13	0.03526
14	0.03425
15	0.03235
16	0.02938
17	0.02869
18	0.02586
19	0.02367
20	0.02178
21	0.02016
22	0.01798
23	0.01629
24	0.01334
25	0.01229
26	0.00967
27	0.00814
28	0.00648
29	0.00416
30	0.00185