Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
leadpipe opened this issue Feb 6, 2025 · 1 comment
Closed
Labels
X-bug Type: bug report

Comments

@leadpipe
Copy link

leadpipe commented Feb 6, 2025

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]);
  }
}
@leadpipe leadpipe added the X-bug Type: bug report label Feb 6, 2025
@dhardy
Copy link
Member

dhardy commented Feb 7, 2025

The CHANGELOG mentions:

New, faster algorithms for SliceRandom::shuffle and partial_shuffle (#1272)

Our reproducibility policy permits value-breaking changes in v0.9. Not what you want to hear, I know, but we do actually have a policy on this stuff.

@dhardy dhardy closed this as completed Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
X-bug Type: bug report
Projects
None yet
Development

No branches or pull requests

2 participants