Skip to content
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

Bug: random.shuffle() function is broken and does not work as expected #353

Open
EgizianoEG opened this issue Sep 18, 2023 · 1 comment
Open

Comments

@EgizianoEG
Copy link

The 'shuffle' function has a bug when it fails to return an array that is shuffled.
The same incorrect behavior is occurring on the documentation object as well.

Reproducible code:

const { shuffle } = require("radash");

const count = {
  123: 0,
  132: 0,
  213: 0,
  231: 0,
  321: 0,
  312: 0,
};

for (let i = 0; i < 1_000_000; i++) {
  const array = [1, 2, 3];
  shuffle(array);
  count[array.join("")]++;
}

for (const key in count) {
  console.log(`${key}: ${count[key]}`);
}

/** 
 * Logs the following:
   123: 1000000
   132: 0
   213: 0
   231: 0
   312: 0
   321: 0
 */
@EgizianoEG EgizianoEG changed the title Bug: random.shuffle() function does not work as expected Bug: random.shuffle() function is broken and does not work as expected Sep 18, 2023
@UnKnoWn-Consortium
Copy link
Contributor

shuffle does not manipulate the original array. Instead the changes are made to a new one that is returned. Your code will work if you make this change:

for (let i = 0; i < 1_000_000; i++) {
  let array = [1, 2, 3];
  array = shuffle(array);
  count[array.join("")]++;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants