1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
pub fn gen_raw<R>(mut rng: R) -> f64
where
R: rand::Rng,
{
rng.sample(rand_distr::StandardNormal)
}
pub fn gen<R>(mut rng: R, value: f64, randomness: f64) -> f64
where
R: rand::Rng,
{
assert!(value >= 0.0);
assert!(value < 1.0);
assert!(randomness >= 0.0);
assert!(randomness <= 1.0);
if randomness == 0.0 {
return value;
}
if randomness == 1.0 {
return rand::Rng::gen_range(&mut rng, 0.0, 1.0);
}
let offset_value = value * 2.0 - 1.0;
loop {
let normal = gen_raw(&mut rng);
let attempt = normal * randomness + offset_value;
if -1.0 <= attempt && attempt < 1.0 {
return (attempt + 1.0) * 0.5;
}
}
}