Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch sample_single int to O'neill's modulo method & update gen_rang… #1154

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions benches/distributions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,14 @@ gen_range_int!(gen_range_i32_low, i32, -1i32, 0);
gen_range_int!(gen_range_i64_low, i64, -1i64, 0);
gen_range_int!(gen_range_i128_low, i128, -1i128, 0);

// These were the initially tested ranges. They are likely to see fewer
// rejections than the low tests.
gen_range_int!(gen_range_i8_high, i8, -20i8, 100);
gen_range_int!(gen_range_i16_high, i16, -500i16, 2000);
gen_range_int!(gen_range_i32_high, i32, -200_000_000i32, 800_000_000);
gen_range_int!(gen_range_i64_high, i64, 3i64, 123_456_789_123);
gen_range_int!(gen_range_i128_high, i128, -12345678901234i128, 123_456_789_123_456_789);
// These are likely to see fewer rejections than the low tests. The starting
// range here is `2^(N - 1) + 1`, the region with the highest rejection chance for
// modulo/bitmask/leading_zeros methods.
gen_range_int!(gen_range_i8_high, i8, i8::min_value(), 1);
gen_range_int!(gen_range_i16_high, i16, i16::min_value(), 1);
gen_range_int!(gen_range_i32_high, i32, i32::min_value(), 1);
gen_range_int!(gen_range_i64_high, i64, i64::min_value(), 1);
gen_range_int!(gen_range_i128_high, i128, i128::min_value(), 1);

// construct and sample from a floating-point range
macro_rules! gen_range_float {
Expand Down
38 changes: 20 additions & 18 deletions src/distributions/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,26 +529,28 @@ macro_rules! uniform_int_impl {
return rng.gen();
}

let zone = if ::core::$unsigned::MAX <= ::core::u16::MAX as $unsigned {
// Using a modulus is faster than the approximation for
// i8 and i16. I suppose we trade the cost of one
// modulus for near-perfect branch prediction.
let unsigned_max: $u_large = ::core::$u_large::MAX;
let ints_to_reject = (unsigned_max - range + 1) % range;
unsigned_max - ints_to_reject
} else {
// conservative but fast approximation. `- 1` is necessary to allow the
// same comparison without bias.
(range << range.leading_zeros()).wrapping_sub(1)
};

loop {
let v: $u_large = rng.gen();
let (hi, lo) = v.wmul(range);
if lo <= zone {
return low.wrapping_add(hi as $ty);
// we use the "Debiased Int Mult (t-opt, m-opt)" rejection sampling method
// described here https://www.pcg-random.org/posts/bounded-rands.html
// and here https://github.com/imneme/bounded-rands

let (mut hi, mut lo) = rng.gen::<$u_large>().wmul(range);
// this shortcut works best with small ranges
if lo < range {
let mut threshold = range.wrapping_neg();
// this shortcut works best with large ranges
if threshold >= range {
threshold -= range;
if threshold >= range {
threshold %= range;
}
}
while lo < threshold {
let (new_hi, new_lo) = rng.gen::<$u_large>().wmul(range);
hi = new_hi;
lo = new_lo;
}
}
low.wrapping_add(hi as $ty)
}
}
};
Expand Down
10 changes: 5 additions & 5 deletions src/seq/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,11 +662,11 @@ mod test {
);
};

do_test(10, 6, &[8, 0, 3, 5, 9, 6]); // floyd
do_test(25, 10, &[18, 15, 14, 9, 0, 13, 5, 24]); // floyd
do_test(300, 8, &[30, 283, 150, 1, 73, 13, 285, 35]); // floyd
do_test(300, 80, &[31, 289, 248, 154, 5, 78, 19, 286]); // inplace
do_test(300, 180, &[31, 289, 248, 154, 5, 78, 19, 286]); // inplace
do_test(10, 6, &[0, 9, 8, 6, 5, 4]); // floyd
do_test(25, 10, &[24, 1, 20, 16, 19, 22, 14, 9]); // floyd
do_test(300, 8, &[30, 283, 243, 150, 218, 240, 1, 189]); // floyd
do_test(300, 80, &[31, 289, 248, 154, 221, 243, 7, 192]); // inplace
do_test(300, 180, &[31, 289, 248, 154, 221, 243, 7, 192]); // inplace

do_test(1_000_000, 8, &[
103717, 963485, 826422, 509101, 736394, 807035, 5327, 632573,
Expand Down
40 changes: 20 additions & 20 deletions src/seq/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,29 +710,29 @@ mod test {
let mut nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

assert_eq!(chars.choose(&mut r), Some(&'l'));
assert_eq!(nums.choose_mut(&mut r), Some(&mut 10));
assert_eq!(nums.choose_mut(&mut r), Some(&mut 3));

#[cfg(feature = "alloc")]
assert_eq!(
&chars
.choose_multiple(&mut r, 8)
.cloned()
.collect::<Vec<char>>(),
&['d', 'm', 'b', 'n', 'c', 'k', 'h', 'e']
&['f', 'i', 'd', 'b', 'c', 'm', 'j', 'k']
);

#[cfg(feature = "alloc")]
assert_eq!(chars.choose_weighted(&mut r, |_| 1), Ok(&'f'));
assert_eq!(chars.choose_weighted(&mut r, |_| 1), Ok(&'l'));
#[cfg(feature = "alloc")]
assert_eq!(nums.choose_weighted_mut(&mut r, |_| 1), Ok(&mut 5));
assert_eq!(nums.choose_weighted_mut(&mut r, |_| 1), Ok(&mut 8));

let mut r = crate::test::rng(414);
nums.shuffle(&mut r);
assert_eq!(nums, [9, 5, 3, 10, 7, 12, 8, 11, 6, 4, 0, 2, 1]);
assert_eq!(nums, [10, 11, 8, 7, 4, 6, 12, 5, 3, 0, 9, 2, 1]);
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
let res = nums.partial_shuffle(&mut r, 6);
assert_eq!(res.0, &mut [7, 4, 8, 6, 9, 3]);
assert_eq!(res.1, &mut [0, 1, 2, 12, 11, 5, 10]);
assert_eq!(res.0, &mut [6, 10, 7, 2, 0, 8]);
assert_eq!(res.1, &mut [11, 1, 12, 3, 4, 5, 9]);
}

#[derive(Clone)]
Expand Down Expand Up @@ -1122,15 +1122,15 @@ mod test {

assert_eq!(choose([].iter().cloned()), None);
assert_eq!(choose(0..100), Some(33));
assert_eq!(choose(UnhintedIterator { iter: 0..100 }), Some(40));
assert_eq!(choose(UnhintedIterator { iter: 0..100 }), Some(54));
assert_eq!(
choose(ChunkHintedIterator {
iter: 0..100,
chunk_size: 32,
chunk_remaining: 32,
hint_total_size: false,
}),
Some(39)
Some(74)
);
assert_eq!(
choose(ChunkHintedIterator {
Expand All @@ -1139,23 +1139,23 @@ mod test {
chunk_remaining: 32,
hint_total_size: true,
}),
Some(39)
Some(74)
);
assert_eq!(
choose(WindowHintedIterator {
iter: 0..100,
window_size: 32,
hint_total_size: false,
}),
Some(90)
Some(34)
);
assert_eq!(
choose(WindowHintedIterator {
iter: 0..100,
window_size: 32,
hint_total_size: true,
}),
Some(90)
Some(34)
);
}

Expand All @@ -1167,16 +1167,16 @@ mod test {
}

assert_eq!(choose([].iter().cloned()), None);
assert_eq!(choose(0..100), Some(40));
assert_eq!(choose(UnhintedIterator { iter: 0..100 }), Some(40));
assert_eq!(choose(0..100), Some(54));
assert_eq!(choose(UnhintedIterator { iter: 0..100 }), Some(54));
assert_eq!(
choose(ChunkHintedIterator {
iter: 0..100,
chunk_size: 32,
chunk_remaining: 32,
hint_total_size: false,
}),
Some(40)
Some(54)
);
assert_eq!(
choose(ChunkHintedIterator {
Expand All @@ -1185,23 +1185,23 @@ mod test {
chunk_remaining: 32,
hint_total_size: true,
}),
Some(40)
Some(54)
);
assert_eq!(
choose(WindowHintedIterator {
iter: 0..100,
window_size: 32,
hint_total_size: false,
}),
Some(40)
Some(54)
);
assert_eq!(
choose(WindowHintedIterator {
iter: 0..100,
window_size: 32,
hint_total_size: true,
}),
Some(40)
Some(54)
);
}

Expand All @@ -1216,7 +1216,7 @@ mod test {

do_test(0..4, &[0, 1, 2, 3]);
do_test(0..8, &[0, 1, 2, 3, 4, 5, 6, 7]);
do_test(0..100, &[58, 78, 80, 92, 43, 8, 96, 7]);
do_test(0..100, &[77, 95, 38, 23, 25, 8, 58, 40]);

#[cfg(feature = "alloc")]
{
Expand All @@ -1227,7 +1227,7 @@ mod test {

do_test(0..4, &[0, 1, 2, 3]);
do_test(0..8, &[0, 1, 2, 3, 4, 5, 6, 7]);
do_test(0..100, &[58, 78, 80, 92, 43, 8, 96, 7]);
do_test(0..100, &[77, 95, 38, 23, 25, 8, 58, 40]);
}
}

Expand Down