diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 21:30:40 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 21:30:40 +0000 |
commit | 133a45c109da5310add55824db21af5239951f93 (patch) | |
tree | ba6ac4c0a950a0dda56451944315d66409923918 /test/lua/pcall_test.lua | |
parent | Initial commit. (diff) | |
download | rspamd-133a45c109da5310add55824db21af5239951f93.tar.xz rspamd-133a45c109da5310add55824db21af5239951f93.zip |
Adding upstream version 3.8.1.upstream/3.8.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/lua/pcall_test.lua')
-rw-r--r-- | test/lua/pcall_test.lua | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/test/lua/pcall_test.lua b/test/lua/pcall_test.lua new file mode 100644 index 0000000..26f3ae4 --- /dev/null +++ b/test/lua/pcall_test.lua @@ -0,0 +1,45 @@ +--[[ https://en.wikipedia.org/wiki/Normal_distribution ]] + +-- The Box–Muller method +local function gaussian(mean, variance) + local U = math.random() + local V = math.random() + return math.sqrt(-2.0 * variance * math.log(U)) * + math.cos(2.0 * math.pi * V) + mean +end + +local function mean(t) + local sum = 0 + local count = #t + for i = 1, count do + sum = sum + t[i] + end + return sum / count +end + +local function std(t, mean) + local squares = 0.0 + for i = 1, #t do + local deviation = math.abs(mean - t[i]) + squares = squares + deviation * deviation + end + local variance = squares / #t + return math.sqrt(variance) +end + +local function do_the_call() + local t = {} + local mu = 34.0 + local sigma = 10.0 + + for i = 1, 5 do + table.insert(t, gaussian(mu, sigma)) + end + + return string.format("Got mean: %1.5f, mu: %1.5f\nstd deviance:%1.5f, expected: %1.5f", + mean(t), mu, + std(t, mu), math.sqrt(sigma)) +end + +math.randomseed(os.time()) +return do_the_call |