Skip to content

Commit e2b4b12

Browse files
committed
Generate 64 bits at a time, instead of 1
1 parent ff176a4 commit e2b4b12

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

rand_distr/src/weighted_fldr.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,22 @@ impl Distribution<i32> for WeightedIndex {
8282
let mut d: i32 = 0;
8383

8484
loop {
85-
let b: bool = rng.gen();
86-
let b = b as i32;
87-
d = 2*d + (1 - b);
88-
if d < h1[c as usize] {
89-
let z = h2[(d*k + c) as usize];
90-
if z < n {
91-
return z;
85+
let r = rng.next_u64();
86+
for bit in 0..64 {
87+
let b = ((r >> bit) & 1) as i32;
88+
d = 2*d + (1 - b);
89+
if d < h1[c as usize] {
90+
let z = h2[(d*k + c) as usize];
91+
if z < n {
92+
return z;
93+
} else {
94+
d = 0;
95+
c = 0;
96+
}
9297
} else {
93-
d = 0;
94-
c = 0;
98+
d -= h1[c as usize];
99+
c += 1;
95100
}
96-
} else {
97-
d -= h1[c as usize];
98-
c += 1;
99101
}
100102
}
101103
}

0 commit comments

Comments
 (0)