Rand v0.6.0

The Rng::shuffle method is now deprecated; rand::seq::SliceRandom trait should be used. It provides the shuffle() method on all slices, which accepts an Rng instance:

// Rust edition 2018 no longer needs extern crate

use rand::thread_rng;
use rand::seq::SliceRandom;

fn main() {
    let mut vec: Vec<u32> = (0..10).collect();
    vec.shuffle(&mut thread_rng());
    println!("{:?}", vec);
}

See it on Playground.

Original answer

You're very close. This should work:

extern crate rand;

use rand::{thread_rng, Rng};

fn main() {
    let mut vec: Vec<u32> = (0..10).collect();
    let slice: &mut [u32] = &mut vec;

    thread_rng().shuffle(slice);
}

&mut [T] is implicitly coercible to &[T], and you annotated the slice variable with &[u32], so the slice became immutable: &mut [u32] was coerced to &[u32]. mut on the variable is not relevant here because slices are just borrows into data owned by someone else, so they do not have inherited mutability - their mutability is encoded in their types.

In fact, you don't need an annotation on slice at all. This works as well:

extern crate rand;

use rand::{thread_rng, Rng};

fn main() {
    let mut vec: Vec<u32> = (0..10).collect();
    let slice = vec.as_mut_slice();

    thread_rng().shuffle(slice);
}

You don't even need the intermediate variable:

extern crate rand;

use rand::{thread_rng, Rng};

fn main() {
    let mut vec: Vec<u32> = (0..10).collect();
    thread_rng().shuffle(&mut vec);
}

You should read The Rust Programming Language as it explains the concepts of ownership and borrowing and how they interact with mutability.


Answer from Vladimir Matveev on Stack Overflow
Top answer
1 of 3
133

Rand v0.6.0

The Rng::shuffle method is now deprecated; rand::seq::SliceRandom trait should be used. It provides the shuffle() method on all slices, which accepts an Rng instance:

// Rust edition 2018 no longer needs extern crate

use rand::thread_rng;
use rand::seq::SliceRandom;

fn main() {
    let mut vec: Vec<u32> = (0..10).collect();
    vec.shuffle(&mut thread_rng());
    println!("{:?}", vec);
}

See it on Playground.

Original answer

You're very close. This should work:

extern crate rand;

use rand::{thread_rng, Rng};

fn main() {
    let mut vec: Vec<u32> = (0..10).collect();
    let slice: &mut [u32] = &mut vec;

    thread_rng().shuffle(slice);
}

&mut [T] is implicitly coercible to &[T], and you annotated the slice variable with &[u32], so the slice became immutable: &mut [u32] was coerced to &[u32]. mut on the variable is not relevant here because slices are just borrows into data owned by someone else, so they do not have inherited mutability - their mutability is encoded in their types.

In fact, you don't need an annotation on slice at all. This works as well:

extern crate rand;

use rand::{thread_rng, Rng};

fn main() {
    let mut vec: Vec<u32> = (0..10).collect();
    let slice = vec.as_mut_slice();

    thread_rng().shuffle(slice);
}

You don't even need the intermediate variable:

extern crate rand;

use rand::{thread_rng, Rng};

fn main() {
    let mut vec: Vec<u32> = (0..10).collect();
    thread_rng().shuffle(&mut vec);
}

You should read The Rust Programming Language as it explains the concepts of ownership and borrowing and how they interact with mutability.


2 of 3
20

You can use shuffle like this:

extern crate rand;

use rand::Rng;

fn main() {
    let mut vec: Vec<usize> = (0..10).collect();
    println!("{:?}", vec);
    rand::thread_rng().shuffle(&mut vec);
    println!("{:?}", vec);
}
Discussions

What's the best way to shuffle an iterator?
Look into reservoir sampling: https://en.m.wikipedia.org/wiki/Reservoir_sampling More on reddit.com
🌐 r/rust
13
12
April 7, 2023
Shuffle Iterator
Background What is your motivation? In a game AI, I'd like to use a random permutation of a vector. However, the order is only used in an iterator and discarded afterwards. Feature request I... More on github.com
🌐 github.com
5
June 17, 2019
Shuffling object vectors around
I wanna switch the content of two vectors in my object through a private method. The example below i am showing is an oversimplification of a more complicated example where shift is not as simple as here but a more complicated arithmetic shuffle (here I am just rewriting vectors): Example: ... More on users.rust-lang.org
🌐 users.rust-lang.org
1
0
February 18, 2020
How can I, using stable rust, mutably iterate over a random shuffle?
This is a follow up to a previous question. I ended up taking the previous solution and updating it to use Rayon for parallel computation. The code now looks like this all_orgs.par_chunks_mut(48).for_each(|chunk: &mut … More on users.rust-lang.org
🌐 users.rust-lang.org
0
0
January 17, 2024
🌐
Rust
docs.rs › shuffle
Crate shuffle - Rust
use shuffle::shuffler::Shuffler; use shuffle::irs::Irs; use rand::rngs::mock::StepRng; let mut rng = StepRng::new(2, 13); let mut irs = Irs::default(); let mut input = vec![1, 2, 3, 4, 5]; irs.shuffle(&mut input, &mut rng); assert_eq!(&input, &[4, 1, 5, 3, 2]);
🌐
Rust
docs.rs › nois › latest › nois › fn.shuffle.html
shuffle in nois - Rust
use nois::{randomness_from_str, shuffle}; let randomness = randomness_from_str("9e8e26615f51552aa3b18b6f0bcf0dae5afbe30321e8d7ea7fa51ebeb1d8fe62").unwrap(); let original = vec![ "bob".to_string(), "mary".to_string(), "su".to_string(), "marc".to_string() ]; let shuffled = shuffle(randomness, original.clone()); // The length of the vector is the same but the order of the elements has changed assert_eq!(shuffled.len(), original.len()); assert_ne!(shuffled, original);
🌐
Reddit
reddit.com › r/rust › what's the best way to shuffle an iterator?
r/rust on Reddit: What's the best way to shuffle an iterator?
April 7, 2023 -

I need to get some random distinct elements inside a range. I don't need to shuffle all the items, so collecting the iterator into a `vec` and calling the ` shuffle` method on it is not desirable. Is there a way to iterate over the range in random order and only get the first `n` items?

EDIT with some context:
I have arbitrarily large collections of `BigInt` numbers and I need to retrieve `n` values in a range from `0` to numbers even of hundreds or thousands of bits. I cannot collect 1 googol items in a `Vec` only to retrieve some tenths or hundreds values. Of course, I could assume that the probability of getting the same number twice is negligible in this specific case but, since the range is arbitrary large I could also have a range of 10 numbers and I must take 9 of them. As a general solution I have started getting random numbers in the target range and manually check if this has been already taken in a previous iteration but I was looking for a lazy solution that could avoid unnecessary checks or allocations.

🌐
Rust Rand
rust-random.github.io › book › guide-seq.html
Sequences - The Rust Rand Book
SliceRandom::partial_shuffle: partial shuffle; useful to extract amount random elements in random order
🌐
The Rust Programming Language
rust-lang.github.io › packed_simd › src › packed_simd_2 › api › shuffle.rs.html
shuffle.rs - source
/// # } /// ``` /// /// Shuffling elements of one vector: /// /// ``` /// # use packed_simd_2::*; /// # fn main() { /// // Shuffle allows reordering the elements of a vector: /// let x = i32x4::new(1, 2, 3, 4); /// let r = shuffle!(x, [2, 1, 3, 0]); /// assert_eq!(r, i32x4::new(3, 2, 4, 1)); ...
🌐
Programming Idioms
programming-idioms.org › idiom › 10 › shuffle-a-list › 410 › rust
Shuffle a list, in Rust
Rust · use std::hash::{BuildHasher, Hasher, RandomState}; pub fn shuffle<T>(vec: &mut [T]) { let n = vec.len(); for i in 0..(n - 1) { let j = rand() % (n - i) + i; vec.swap(i, j); } } pub fn rand() -> usize { RandomState::new().build_hasher().finish() as usize } C ·
Find elsewhere
🌐
crates.io
crates.io › crates › shuffle
shuffle - crates.io: Rust Package Registry
Implementation of various shuffling algorithms over slices.
🌐
GitHub
github.com › rust-random › rand › issues › 826
Shuffle Iterator · Issue #826 · rust-random/rand
June 17, 2019 - Background What is your motivation? In a game AI, I'd like to use a random permutation of a vector. However, the order is only used in an iterator and discarded afterwards. Feature request I'd like to have a struct RandomizedOrder tha...
Author   ThomasdenH
🌐
The Rust Programming Language
rust-lang.github.io › packed_simd › packed_simd_2 › macro.shuffle.html
shuffle in packed_simd_2 - Rust
The indices i in range [0, N) refer to the i-th element of vec0, while the indices in range [N, 2*N) refer to the i - N-th element of vec1. ... // Shuffle allows reordering the elements: let x = i32x4::new(1, 2, 3, 4); let y = i32x4::new(5, 6, 7, 8); let r = shuffle!(x, y, [4, 0, 5, 1]); assert_eq!(r, i32x4::new(5, 1, 6, 2)); // The resulting vector can als be smaller than the input: let r = shuffle!(x, y, [1, 6]); assert_eq!(r, i32x2::new(2, 7)); // Or larger: let r = shuffle!(x, y, [1, 3, 4, 2, 1, 7, 2, 2]); assert_eq!(r, i32x8::new(2, 4, 5, 3, 2, 8, 3, 3)); // At most 2 * the number of lanes in the input vector.
🌐
Rust
docs.rs › shuffle › latest › shuffle
shuffle - Rust
use shuffle::shuffler::Shuffler; use shuffle::irs::Irs; use rand::rngs::mock::StepRng; let mut rng = StepRng::new(2, 13); let mut irs = Irs::default(); let mut input = vec![1, 2, 3, 4, 5]; irs.shuffle(&mut input, &mut rng); assert_eq!(&input, &[4, 1, 5, 3, 2]);
🌐
Rust
docs.rs › packed_simd › latest › packed_simd › macro.shuffle.html
shuffle in packed_simd - Rust
The indices i in range [0, N) refer to the i-th element of vec0, while the indices in range [N, 2*N) refer to the i - N-th element of vec1. ... // Shuffle allows reordering the elements: let x = i32x4::new(1, 2, 3, 4); let y = i32x4::new(5, 6, 7, 8); let r = shuffle!(x, y, [4, 0, 5, 1]); assert_eq!(r, i32x4::new(5, 1, 6, 2)); // The resulting vector can als be smaller than the input: let r = shuffle!(x, y, [1, 6]); assert_eq!(r, i32x2::new(2, 7)); // Or larger: let r = shuffle!(x, y, [1, 3, 4, 2, 1, 7, 2, 2]); assert_eq!(r, i32x8::new(2, 4, 5, 3, 2, 8, 3, 3)); // At most 2 * the number of lanes in the input vector.
🌐
Rust Programming Language
users.rust-lang.org › t › how-can-i-using-stable-rust-mutably-iterate-over-a-random-shuffle › 105457
How can I, using stable rust, mutably iterate over a random shuffle? - The Rust Programming Language Forum
January 17, 2024 - This is a follow up to a previous question. I ended up taking the previous solution and updating it to use Rayon for parallel computation. The code now looks like this all_orgs.par_chunks_mut(48).for_each(|chunk: &mut [Organism]|{ for i in 0 .. chunk.len() { let (left, others) = chunk.split_at_mut(i); let (middle, right) = others.split_at_mut(1); let org1 = &mut middle[0]; //process left for org2 in left { single_match_up(org1, org2); ...
🌐
crates.io
crates.io › crates › rip_shuffle
rip_shuffle - crates.io: Rust Package Registry
October 28, 2023 - use rip_shuffle::RipShuffleSequential; let mut data : Vec<_> = (0..100).into_iter().collect(); data.seq_shuffle(&mut rand::thread_rng());
🌐
GitHub
github.com › rust-lang-nursery › packed_simd › issues › 102
Appropriate name for single vector shuffle · Issue #102 · rust-lang/packed_simd
August 29, 2018 - Shuffles for single vectors are currently called permute! (when the indices are compile-time constants) and permute_dyn (when the indices are provided with a run-time vector of the same vector size). These shuffles are allowed to repeat ...
Author   gnzlbg
🌐
Python Examples
pythonexamples.org › rust › how-to-shuffle-a-vector
How to Shuffle a Vector in Rust
extern crate rand; use rand::seq::SliceRandom; fn main() { let mut int_vector = vec![1, 2, 3, 4, 5]; int_vector.shuffle(&mut rand::thread_rng()); println!("{:?}", int_vector); }Copy
Top answer
1 of 2
2

Correct me if I'm wrong: you're looking for a way to retain n random elements in a Vec and discard the rest. In that case, the easiest way would be to use partial_shuffle(), a rand function implemented for slices.

Shuffle a slice in place, but exit early.

Returns two mutable slices from the source slice. The first contains amount elements randomly permuted. The second has the remaining elements that are not fully shuffled.

use rand::{thread_rng, seq::SliceRandom};

fn main() {
    let mut rng = thread_rng();
    
    // Use the `RangeInclusive` (`..=`) syntax at times like this.
    for n in 1..=10 {
        let mut elements: Vec<u8> = (1..=10).collect();
        let (elements, _rest) = elements.as_mut_slice().partial_shuffle(&mut rng, n);
        println!("{n}: {elements:?}");
    }
}

Run this snippet on Rust Playground.

elements is shadowed, going from a Vec to a &mut [T]. If you're only going to use it inside the function, that's probably all you'll need. However, since it's a reference, you can't return it; the data it's pointing to is owned by the original vector, which will be dropped when it goes out of scope. If that's what you need, you'll have to turn the slice into a Vec.

While you can simply construct a new one from it using Vec::from, I suspect (but haven't tested) that it's more efficient to use Vec::split_off.

Splits the collection into two at the given index.

Returns a newly allocated vector containing the elements in the range [at, len). After the call, the original vector will be left containing the elements [0, at) with its previous capacity unchanged.

use rand::{thread_rng, seq::SliceRandom};

fn main() {
    let mut rng = thread_rng();
    
    for n in 1..=10 {
        let mut elements: Vec<u8> = (1..=10).collect();
        elements.as_mut_slice().partial_shuffle(&mut rng, n);
        let elements = elements.split_off(elements.len() - n);
        // `elements` is still a `Vec`; this time, containing only
        // the shuffled elements. You can use it as the return value.
        println!("{n}: {elements:?}");
    }
}

Run this snippet on Rust Playground.


Since this function lives on a performance-critical path, I'd recommend benchmarking it against your current implementation. At the time of writing this, criterion is the most popular way to do that. That said, rand is an established library, so I imagine it will perform as well or better than a manual implementation.

Sample Benchmark

I don't know what kind of numbers you're working with, but here's a sample benchmark with for n in 1..=100 and (1..=100).collect() (i.e. 100 instead of 10 in both places) without the print statements:

manual time: [73.683 µs 73.749 µs 73.821 µs]
rand with slice time: [68.074 µs 68.147 µs 68.226 µs]
rand with vec time: [54.147 µs 54.213 µs 54.288 µs]

Bizarrely, splitting off a Vec performed vastly better than not. Unless I made an error in my benchmarks, the compiler is probably doing something under the hood that you'll need a more experienced Rustacean than me to explain.

Benchmark Implementation

Cargo.toml

[dependencies]
rand = "0.8.5"

[dev-dependencies]
criterion = "0.4.0"

[[bench]]
name = "rand_benchmark"
harness = false

[[bench]]
name = "rand_vec_benchmark"
harness = false

[[bench]]
name = "manual_benchmark"
harness = false

benches/manual_benchmark.rs

use criterion::{criterion_group, criterion_main, Criterion};

fn manual_solution() {
    for n in 1..=100 {
        let mut elements: Vec<u8> = (1..=100).collect();
        keep_n_rand(&mut elements, n);
    }
}

fn keep_n_rand<T>(elements: &mut Vec<T>, n: usize) {
    use rand::{thread_rng, Rng};

    let mut rng = thread_rng();

    for i in n..elements.len() {
        let j = rng.gen_range(0..i);

        if j < n {
            elements.swap(i, j);
        }
    }

    elements.truncate(n);
}

fn benchmark(c: &mut Criterion) {
    c.bench_function("manual", |b| b.iter(manual_solution));
}

criterion_group!(benches, benchmark);
criterion_main!(benches);

benches/rand_benchmark.rs

use criterion::{criterion_group, criterion_main, Criterion};

fn rand_solution() {
    use rand::{seq::SliceRandom, thread_rng};

    let mut rng = thread_rng();

    for n in 1..=100 {
        let mut elements: Vec<u8> = (1..=100).collect();
        let (_elements, _) = elements.as_mut_slice().partial_shuffle(&mut rng, n);
    }
}

fn benchmark(c: &mut Criterion) {
    c.bench_function("rand with slice", |b| b.iter(rand_solution));
}

criterion_group!(benches, benchmark);
criterion_main!(benches);

benches/rand_vec_benchmark.rs

use criterion::{criterion_group, criterion_main, Criterion};

fn rand_solution() {
    use rand::{seq::SliceRandom, thread_rng};

    let mut rng = thread_rng();

    for n in 1..=100 {
        let mut elements: Vec<u8> = (1..=100).collect();
        elements.as_mut_slice().partial_shuffle(&mut rng, n);
        let _elements = elements.split_off(elements.len() - n);
    }
}

fn benchmark(c: &mut Criterion) {
    c.bench_function("rand with vec", |b| b.iter(rand_solution));
}

criterion_group!(benches, benchmark);
criterion_main!(benches);
2 of 2
1

Is that possible and desirable in rust?

It is not possible unless you constrain T: Copy or T: Clone: while C++ uses non-destructive moves (the source is in a valid but unspecified state) Rust uses destructive moves (the source is gone).

There are ways around it using unsafe but they require being very careful and it's probably not worth the hassle (you can look at Vec::swap_remove for a taste, it basically does what you're doing here except only between j and the last element of the vec).

I'd also recommend verified_tinker's solution, as I'm not convinced your shuffle is unbiased.