#algorithm #shuffling #slice #random #fisher-yates #shuffler #irs

shuffle

Implementation of various shuffling algorithms over slices

6 releases

0.1.7 Sep 30, 2021
0.1.6 Sep 29, 2021
0.1.3 Oct 2, 2019

#1262 in Algorithms

Download history 134/week @ 2024-04-07 138/week @ 2024-04-14 158/week @ 2024-04-21 111/week @ 2024-04-28 156/week @ 2024-05-05 163/week @ 2024-05-12 188/week @ 2024-05-19 160/week @ 2024-05-26 165/week @ 2024-06-02 83/week @ 2024-06-09 150/week @ 2024-06-16 174/week @ 2024-06-23 108/week @ 2024-06-30 83/week @ 2024-07-07 122/week @ 2024-07-14 132/week @ 2024-07-21

477 downloads per month
Used in 10 crates (6 directly)

MIT license

12KB
201 lines

shuffle

Various shuffling algorithms for rust.

Currently implemented shuffling algorithms

  • Inverse Riffle Shuffle
  • Fisher-Yates
  • ... ? TODO

Examples

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]);

lib.rs:

Crate implementing various kinds of shuffling algorithms such as Inverse Riffle Shuffle (more algorithms coming soon).

Why

Currently, the most common way of shuffling a collection is by using rand::shuffle, which is basically Fisher-Yates algorithm. This is nice, but it requires that you have a good source of random numbers in an arbitrary range.

This crate aims to provide good abstractions to shuffle collections when all you have is just a source of randomness. (but we also implement Fisher-Yates, because why not?)

Assuming that the source of the randomness is good, all of the shuffling algorithms return a permutation from a uniform distribution.

Example

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]);

Dependencies

~1MB
~26K SLoC