Title: [BUG]: Biased shuffle implementation in bogoSort (incorrect Fisher–Yates range) · Issue #1867 · TheAlgorithms/JavaScript · GitHub
Open Graph Title: [BUG]: Biased shuffle implementation in bogoSort (incorrect Fisher–Yates range) · Issue #1867 · TheAlgorithms/JavaScript
X Title: [BUG]: Biased shuffle implementation in bogoSort (incorrect Fisher–Yates range) · Issue #1867 · TheAlgorithms/JavaScript
Description: Description The current shuffle function used by the bogoSort implementation appears to implement a biased version of the Fisher–Yates shuffle. Specifically, the random index is selected from an incorrect range, which prevents some permu...
Open Graph Description: Description The current shuffle function used by the bogoSort implementation appears to implement a biased version of the Fisher–Yates shuffle. Specifically, the random index is selected from an in...
X Description: Description The current shuffle function used by the bogoSort implementation appears to implement a biased version of the Fisher–Yates shuffle. Specifically, the random index is selected from an in...
Opengraph URL: https://github.com/TheAlgorithms/JavaScript/issues/1867
X: @github
Domain: Github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[BUG]: Biased shuffle implementation in bogoSort (incorrect Fisher–Yates range)","articleBody":"### Description\n\nThe current `shuffle` function used by the `bogoSort` implementation appears to implement a biased version of the Fisher–Yates shuffle.\n\nSpecifically, the random index is selected from an incorrect range, which prevents some permutations from ever occurring. This results in a non-uniform shuffle and may affect the theoretical correctness of randomized algorithms such as `bogoSort`.\n\n### Expected Behavior\n\nThe shuffle function should implement an unbiased Fisher–Yates algorithm, where each permutation of the array is equally likely.\n\nThis requires selecting the random index from the inclusive range [0, i] during each iteration.\n\n### Actual Behavior\n\nThe current implementation selects the random index from ` [0, i) `, which introduces bias and makes some permutations impossible.\n\nAs a result:\n\n- The shuffle is not uniform\n- The sorted permutation may be unreachable for certain inputs\n- The expected probabilistic behaviour of `bogoSort` is violated\n\n### Steps to reproduce (if applicable)\n\n1. Use the current shuffle implementation\n2. Shuffle a small array (e.g. [1, 2, 3]) many times\n3. Count permutation frequencies\n4. Observe that some permutations never appear or appear significantly less often\n\n### Additional information\n\n#### Suggested fix\n\nUpdate the shuffle to use the standard Fisher–Yates algorithm:\n\n```js\nfunction shuffle(array) {\n for (let i = array.length - 1; i \u003e 0; i--) {\n const j = Math.floor(Math.random() * (i + 1));\n [array[i], array[j]] = [array[j], array[i]];\n }\n}\n```","author":{"url":"https://github.com/Ayoub-FG","@type":"Person","name":"Ayoub-FG"},"datePublished":"2025-12-23T19:42:37.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/1867/JavaScript/issues/1867"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:76a5ac8a-2518-b77e-21a9-278be57fd299 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E9FE:4DDA3:66E969:8CCF34:6A6275C3 |
| html-safe-nonce | 846da0cfabb84c75f1e8d72274385489ba54b254ccb97c122e1e469083cd5e16 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOUZFOjREREEzOjY2RTk2OTo4Q0NGMzQ6NkE2Mjc1QzMiLCJ2aXNpdG9yX2lkIjoiNjUwODE5Mzc5NTQzNjM0NDc3MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b755969dca9fd335ad2d6e43bcf646dd1af744eb3a2bcb5aaefc6dc6511b8a3d |
| hovercard-subject-tag | issue:3758372651 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/TheAlgorithms/JavaScript/1867/issue_layout |
| twitter:image | https://opengraph.githubassets.com/067676eb2581306548b5e7dd0cea4112d42c0b20235b5f84dccccfda4888f29c/TheAlgorithms/JavaScript/issues/1867 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/067676eb2581306548b5e7dd0cea4112d42c0b20235b5f84dccccfda4888f29c/TheAlgorithms/JavaScript/issues/1867 |
| og:image:alt | Description The current shuffle function used by the bogoSort implementation appears to implement a biased version of the Fisher–Yates shuffle. Specifically, the random index is selected from an in... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Ayoub-FG |
| hostname | github.com |
| expected-hostname | github.com |
| None | 838e87bd0e4f5415707a930bc14e89e382dc2ea046e50ff575252eb2f2cb06a2 |
| turbo-cache-control | no-preview |
| go-import | github.com/TheAlgorithms/JavaScript git https://github.com/TheAlgorithms/JavaScript.git |
| octolytics-dimension-user_id | 20487725 |
| octolytics-dimension-user_login | TheAlgorithms |
| octolytics-dimension-repository_id | 97086543 |
| octolytics-dimension-repository_nwo | TheAlgorithms/JavaScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 97086543 |
| octolytics-dimension-repository_network_root_nwo | TheAlgorithms/JavaScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f0081192f88523d2bf4cfdf22bb3e138affc4cd1 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width