Skip to content

Commit

Permalink
Generate 64 bits at a time, instead of 1
Browse files Browse the repository at this point in the history
  • Loading branch information
vks committed Aug 6, 2020
1 parent ff176a4 commit e2b4b12
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions rand_distr/src/weighted_fldr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,22 @@ impl Distribution<i32> for WeightedIndex {
let mut d: i32 = 0;

loop {
let b: bool = rng.gen();
let b = b as i32;
d = 2*d + (1 - b);
if d < h1[c as usize] {
let z = h2[(d*k + c) as usize];
if z < n {
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 {
return z;
} else {
d = 0;
c = 0;
}
} else {
d = 0;
c = 0;
d -= h1[c as usize];
c += 1;
}
} else {
d -= h1[c as usize];
c += 1;
}
}
}
Expand Down

0 comments on commit e2b4b12

Please sign in to comment.