1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

rand.dist: add exponential distribution function and unit tests (#9402)

This commit is contained in:
Subhomoy Haldar
2021-03-21 16:34:43 +05:30
committed by GitHub
parent c4e6ef424e
commit 0e80e57aa5
2 changed files with 36 additions and 0 deletions

View File

@ -109,3 +109,26 @@ fn test_normal() {
}
}
}
fn test_exponential() {
lambdas := [1.0, 10, 1 / 20.0, 1 / 10000.0, 1 / 524.0, 200]
for seed in seeds {
rand.seed(seed)
for lambda in lambdas {
mu := 1 / lambda
variance := mu * mu
mut sum := 0.0
mut var := 0.0
for _ in 0 .. count {
x := dist.exponential(lambda)
sum += x
dist := x - mu
var += dist * dist
}
assert math.abs((f64(sum / count) - mu) / mu) < error
assert math.abs((f64(var / count) - variance) / variance) < 2 * error
}
}
}