Skip to content

SliceRandom.shuffle produces a different result in 0.9 than in 0.8 #1580

@leadpipe

Description

@leadpipe

Summary

SliceRandom's shuffle function was reimplemented in 0.9, and it produces a different result when using a deterministic PRNG.

I expect rand's functions to preserve results from one release to the next, for deterministic PRNGs.

Code samples

0.8 version

/// The pseudo-random number generator we use.
pub type Random = rand_pcg::Pcg64Mcg;

/// Constructs a new Random from a given string seed.
pub fn new_random(seed: &str) -> Random {
  rand_seeder::Seeder::from(seed).make_rng()
}

#[cfg(test)]
mod tests {
  use rand::seq::SliceRandom;

use super::*;

  #[test]
  fn test_random() {
    let mut rng = new_random("test");
    let mut nums = [1, 2, 3, 4, 5];
    nums.shuffle(&mut rng);
    assert_eq!(nums, [4, 3, 1, 5, 2]);
  }
}

0.9 version

/// The pseudo-random number generator we use.
pub type Random = rand_pcg::Pcg64Mcg;

/// Constructs a new Random from a given string seed.
pub fn new_random(seed: &str) -> Random {
  rand_seeder::Seeder::from(seed).into_rng()
}

#[cfg(test)]
mod tests {
  use rand::seq::SliceRandom;

use super::*;

  #[test]
  fn test_random() {
    let mut rng = new_random("test");
    let mut nums = [1, 2, 3, 4, 5];
    nums.shuffle(&mut rng);
//    assert_eq!(nums, [4, 3, 1, 5, 2]);  -- this fails.  The new shuffle produces this other sequence:
    assert_eq!(nums, [5, 1, 3, 2, 4]);
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    X-bugType: bug report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions