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);
}
🌐
Rust Programming Language
users.rust-lang.org › help
[Solved] Efficient rust random shuffle of? - help - The Rust Programming Language Forum
February 20, 2019 - Google suggests: iterator - How do I create a Vec from a range and shuffle it? - Stack Overflow extern crate rand; use rand::{thread_rng, Rng}; fn main() { let mut vec: Vec = (0..10).collect(); let slice: &mut [u32] = &mut 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 guess this is a trivial one but I have no idea how to make it work. So I wanna save space and thus not make any extra copies of anything if I do not need to. I wanna switch the content of two vectors in my object through a private method. The example below i am showing is an oversimplification ... 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
Crate implementing various kinds of shuffling algorithms such as Inverse Riffle Shuffle (more algorithms coming soon).
🌐
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.

🌐
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)); ...
🌐
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);
🌐
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&#3...
Author   ThomasdenH
🌐
The Rust Programming Language
rust-lang.github.io › packed_simd › packed_simd_2 › macro.shuffle.html
shuffle in packed_simd_2 - Rust
// 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)); // The resulting vector can be smaller than the input: let r = shuffle!(x, [1, 3]); assert_eq!(r, i32x2::new(2, 4)); // Equal: let ...
Find elsewhere
🌐
Rust
docs.rs › packed_simd › latest › packed_simd › macro.shuffle.html
shuffle in packed_simd - Rust
macro_rules! shuffle { ($vec0:expr, $vec1:expr, [$l0:expr, $l1:expr]) => { ... }; ($vec0:expr, $vec1:expr, [$l0:expr, $l1:expr, $l2:expr, $l3:expr]) => { ... }; ($vec0:expr, $vec1:expr, [$l0:expr, $l1:expr, $l2:expr, $l3:expr, $l4:expr, $l5:expr, $l6:expr, $l7:expr]) => { ...
🌐
Programming Idioms
programming-idioms.org › idiom › 10 › shuffle-a-list › 410 › rust
Shuffle a list, in Rust
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 }
🌐
Rust
docs.rs › shuffle › latest › shuffle
shuffle - Rust
This crate aims to provide good abstractions to shuffle collections when all you have is just a source of randomness.
🌐
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 › shuffle
shuffle - crates.io: Rust Package Registry
Implementation of various shuffling algorithms over slices.
🌐
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
🌐
Rust Rand
rust-random.github.io › book › guide-seq.html
Sequences - The Rust Rand Book
Rust · Coal · Navy · Ayu · Rand implements a few common random operations on sequences via the IteratorRandom and SliceRandom traits. To sample: a single index within a given range, use Rng::random_range · multiple distinct indices from 0..length, use index::sample · multiple distinct indices from 0..length with weights, use index::sample_weighted · To shuffle a slice: SliceRandom::shuffle: fully shuffle a slice ·
🌐
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
🌐
GitHub
github.com › MattWoelk › shuffle_vector
GitHub - MattWoelk/shuffle_vector: A Shuffle Vector, as described by https://beta.observablehq.com/@jobleonard/shuffle-vectors
A rust implementation of a Shuffle Vector, as described by https://beta.observablehq.com/@jobleonard/shuffle-vectors.
Author   MattWoelk