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: redirect.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:34e38a93-fe33-a862-12b3-a53cae258200 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D704:21279A:44B9F0:58E484:696B09E6 |
| html-safe-nonce | 62e901a88defa1cc69436fd01baa33e87fe01a2d6ffa559f7cb56d5e81718ecf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzA0OjIxMjc5QTo0NEI5RjA6NThFNDg0OjY5NkIwOUU2IiwidmlzaXRvcl9pZCI6IjI2NTUxNDkzNTUzODY0NzI5MzQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8f03801bb49a6d55cd860e428bfe06113fd35a9a04a5d434336f34fc6d94b04f |
| 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 | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| 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 | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width