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 = 10
  31. for i = 1, count do
  32. local res = crazy_dice(sides) - 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 = -sides, sides do
  39. print(i, (tally[i] or 1) / count)
  40. end
Success #stdin #stdout 2.01s 2540KB
stdin
Standard input is empty
stdout
-10	0.0024
-9	0.00768
-8	0.01485
-7	0.02329
-6	0.03292
-5	0.04487
-4	0.05633
-3	0.07167
-2	0.08654
-1	0.09928
0	0.11565
1	0.10331
2	0.08635
3	0.07147
4	0.05855
5	0.04383
6	0.03403
7	0.02277
8	0.01411
9	0.00774
10	0.00236