fork download
  1.  
  2. math.randomseed(os.time())
  3.  
  4. local result_pos_t = {}
  5. local function result_pos(num, max)
  6. local k = table.concat({...}, ',')
  7. if not result_pos_t[k]
  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
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
luac: prog.lua:6: cannot use '...' outside a vararg function near '...'
stdout
Standard output is empty