// This algorithm has been found on stack overflow <https://stackoverflow.com/a/25238398>.
// Its correctness has been empirically validated
// The uniformity of the generated derangments' distribution has yet to be
// tested/validated/proven.
This algorithm has been found on stack overflow .
Its correctness has been empirically validated
The uniformity of the generated derangments' distribution has yet to be
tested/validated/proven.
This algorithm has been found on stack overflow . Its correctness has been empirically validated The uniformity of the generated derangments ' distribution has yet to be tested / validated / proven .
pub fn random_derangement(n: usize) -> Vec<usize> {
    let mut rng = thread_rng();
    'outer: loop {
        let mut range: Vec<usize> = (0..n).collect();
        for i in (0..n).rev() {
            let p = rng.gen_range(0, i + 1);
            if range[p] == i {
                continue 'outer;
            }
            range.swap(i, p);
        }
        if range[0] != 0 {
            return range;
        }
    }
}
pub fn random_derangement ( n : usize ) -> Vec < usize > { let mut rng = thread_rng ( ) ; ' outer : loop { let mut range : Vec < usize > = ( 0 .. n ) . collect ( ) ; for i in ( 0 .. n ) . rev ( ) { let p = rng . gen_range ( 0 , i + 1 ) ; if range [ p ] == i { continue ' outer ; } range . swap ( i , p ) ; } if range [ 0 ] != 0 { return range ; } } }
This algorithm has been found on stack overflow <https://stackoverflow.com/a/25238398>.
This algorithm has been found on stack overflow <https : // stackoverflow . com / a / 25238398 > .
n
usize