We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent ff176a4 commit e2b4b12Copy full SHA for e2b4b12
rand_distr/src/weighted_fldr.rs
@@ -82,20 +82,22 @@ impl Distribution<i32> for WeightedIndex {
82
let mut d: i32 = 0;
83
84
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;
+ let r = rng.next_u64();
+ for bit in 0..64 {
+ let b = ((r >> bit) & 1) as i32;
+ d = 2*d + (1 - b);
+ if d < h1[c as usize] {
+ let z = h2[(d*k + c) as usize];
+ if z < n {
92
+ return z;
93
+ } else {
94
+ d = 0;
95
+ c = 0;
96
+ }
97
} else {
- d = 0;
- c = 0;
98
+ d -= h1[c as usize];
99
+ c += 1;
100
}
- } else {
- d -= h1[c as usize];
- c += 1;
101
102
103
0 commit comments