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)
  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 1.04s 2540KB
stdin
Standard input is empty
stdout
0	0.16827
1	0.14805
2	0.13695
3	0.12267
4	0.10737
5	0.08996
6	0.075
7	0.06083
8	0.04505
9	0.03089
10	0.01496