Skip to content

Commit dff8c1a

Browse files
Tidied comments
1 parent 5dcdfcb commit dff8c1a

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

src/seq/increasing_uniform.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
use crate::{Rng, RngCore};
22

3-
/// Similar to a Uniform distribution, but after returning a number in the range [0,n], n is increased by 1.
3+
/// Similar to a Uniform distribution,
4+
/// but after returning a number in the range [0,n], n is increased by 1.
45
pub(crate) struct IncreasingUniform<R: RngCore> {
56
pub rng: R,
67
n: u32,
7-
//TODO(opt): this should depend on RNG word size
8-
chunk: u32, //Chunk is a random number in [0, (n + 1) * (n + 2) *..* (n + chunk_remaining) )
8+
// Chunk is a random number in [0, (n + 1) * (n + 2) *..* (n + chunk_remaining) )
9+
chunk: u32,
910
chunk_remaining: u8,
1011
}
1112

1213
impl<R: RngCore> IncreasingUniform<R> {
1314
/// Create a dice roller.
1415
/// The next item returned will be a random number in the range [0,n]
1516
pub fn new(rng: R, n: u32) -> Self {
16-
let chunk_remaining = if n == 0 { 1 } else { 0 }; // If n = 0, the first number returned will always be 0, so we don't need to generate a random number
17+
// If n = 0, the first number returned will always be 0
18+
// so we don't need to generate a random number
19+
let chunk_remaining = if n == 0 { 1 } else { 0 };
1720
Self {
1821
rng,
1922
n,
@@ -30,22 +33,24 @@ impl<R: RngCore> IncreasingUniform<R> {
3033
let next_n = self.n + 1;
3134

3235
let next_chunk_remaining = self.chunk_remaining.checked_sub(1).unwrap_or_else(|| {
33-
//If the chunk is empty, generate a new chunk
36+
// If the chunk is empty, generate a new chunk
3437
let (bound, remaining) = calculate_bound_u32(next_n);
35-
//bound = (n + 1) * (n + 2) *..* (n + remaining)
38+
// bound = (n + 1) * (n + 2) *..* (n + remaining)
3639
self.chunk = self.rng.gen_range(0..bound);
37-
// Chunk is a random number in [0, (n + 1) * (n + 2) *..* (n + remaining) )
40+
// Chunk is a random number in
41+
// [0, (n + 1) * (n + 2) *..* (n + remaining) )
3842

3943
remaining - 1
4044
});
4145

4246
let result = if next_chunk_remaining == 0 {
43-
//If the chunk is empty we asr
44-
self.chunk as usize
4547
// `chunk` is a random number in the range [0..n+1)
46-
// Because `chunk_remaining` is about to be set to zero, we do not need to clear the chunk here
48+
// Because `chunk_remaining` is about to be set to zero
49+
// we do not need to clear the chunk here
50+
self.chunk as usize
4751
} else {
48-
// `chunk` is a random number in a range that is a multiple of n+1 so r will be a random number in [0..n+1)
52+
// `chunk` is a random number in a range that is a multiple of n+1
53+
// so r will be a random number in [0..n+1)
4954
let r = self.chunk % next_n;
5055
self.chunk /= next_n;
5156
r as usize
@@ -70,15 +75,18 @@ fn calculate_bound_u32(m: u32) -> (u32, u8) {
7075
product = p;
7176
current += 1;
7277
} else {
73-
let count = (current - m) as u8; //Maximum value of 13 for when min is 1 or 2
78+
// Count has a maximum value of 13 for when min is 1 or 2
79+
let count = (current - m) as u8;
7480
return (product, count);
7581
}
7682
}
7783
}
7884

7985
const RESULT2: (u32, u8) = inner(2);
8086
if m == 2 {
81-
return RESULT2; //Making this value a constant instead of recalculating it gives a significant (~50%) performance boost for small shuffles
87+
// Making this value a constant instead of recalculating it
88+
// gives a significant (~50%) performance boost for small shuffles
89+
return RESULT2;
8290
}
8391

8492
inner(m)

src/seq/mod.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -636,24 +636,12 @@ impl<T> SliceRandom for [T] {
636636
// The algorithm below is based on Durstenfeld's algorithm for the
637637
// [Fisher–Yates shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm)
638638
// for an unbiased permutation.
639-
// It ensures that the last `amount` elements of the slice are randomly selected from the whole slice.
640-
641-
// The loop invariant is that elements in the range [m,i) are randomly selected from the first i elements.
642-
// This is true initially because the range is empty as i = m.
643-
// The loop body swaps the element at i with a random one of the first i + 1 elements, then increments i.
644-
// After the swap, the element at i will be randomly selected from the first i + 1 elements.
645-
// Because of the invariant, before the swap, every element in the range [m,i) was randomly selected from the first i elements.
646-
// Each of those elements has a 1 in i chance of being swapped with the element at i.
647-
// Therefore, after the swap, the elements in the range [m,i) will be randomly selected from the first i + 1 elements.
648-
// But the element at i was also randomly selected from the first i + 1 elements.
649-
// So the elements in the range [m,i+1) are all randomly selected from the first i + 1 elements.
650-
// So before we increment i, the elements in the range [m,i + 1) are randomly selected from the first i + 1 elements.
651-
// Therefore, the loop invariant holds.
652-
// When the loop exits, elements in the range [m,length] will be randomly selected from the whole slice.
639+
// It ensures that the last `amount` elements of the slice
640+
// are randomly selected from the whole slice.
653641

654-
655-
//`IncreasingUniform::next_index()` is about twice as fast as `gen_index` but only works for 32 bit integers
656-
//Therefore we must use the slow method if the slice is longer than that.
642+
//`IncreasingUniform::next_index()` is faster than `gen_index`
643+
//but only works for 32 bit integers
644+
//So we must use the slow method if the slice is longer than that.
657645
if self.len() < (u32::MAX as usize) {
658646
let mut chooser = IncreasingUniform::new(rng, m as u32);
659647
for i in m..self.len() {

0 commit comments

Comments
 (0)