You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pcall_test.lua 983B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. --[[ https://en.wikipedia.org/wiki/Normal_distribution ]]
  2. -- The Box–Muller method
  3. local function gaussian(mean, variance)
  4. local U = math.random()
  5. local V = math.random()
  6. return math.sqrt(-2.0 * variance * math.log(U)) *
  7. math.cos(2.0 * math.pi * V) + mean
  8. end
  9. local function mean(t)
  10. local sum = 0
  11. local count = #t
  12. for i = 1, count do
  13. sum = sum + t[i]
  14. end
  15. return sum / count
  16. end
  17. local function std(t, mean)
  18. local squares = 0.0
  19. for i = 1, #t do
  20. local deviation = math.abs(mean - t[i])
  21. squares = squares + deviation * deviation
  22. end
  23. local variance = squares / #t
  24. return math.sqrt(variance)
  25. end
  26. local function do_the_call()
  27. local t = {}
  28. local mu = 34.0
  29. local sigma = 10.0
  30. for i = 1, 5 do
  31. table.insert(t, gaussian(mu, sigma))
  32. end
  33. return string.format("Got mean: %1.5f, mu: %1.5f\nstd deviance:%1.5f, expected: %1.5f",
  34. mean(t), mu,
  35. std(t, mu), math.sqrt(sigma))
  36. end
  37. math.randomseed(os.time())
  38. return do_the_call