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 / 2)
  33. if not tally[res] then tally[res] = 1
  34. else tally[res] = tally[res] + 1
  35. end
  36. end
  37.  
  38. print(sides, sides / 2)
  39. for i = -5, sides do
  40. print(i, (tally[i] or 1) / count)
  41. end
Success #stdin #stdout 1.76s 2540KB
stdin
Standard input is empty
stdout
10	5
-5	0.00803
-4	0.02281
-3	0.04529
-2	0.07342
-1	0.10359
0	0.14121
1	0.12576
2	0.11125
3	0.09688
4	0.08047
5	0.06482
6	0.05075
7	0.03578
8	0.02326
9	0.01205
10	0.00463