Fisher-Yates Shuffle Algorithm in JavaScript

preview_player
Показать описание
Today we're going to look at a classic coding algorithm called the Fisher-Yates Shuffle, which is used for randomizing the items of an array. You can read all about it on Wikipedia, but instead, we're going to just dive right in and look at a practical implementation of it!
Рекомендации по теме
Комментарии
Автор

Thanks Bryan. I've created a simple FY Javascript function that runs one shuffle only on any given character string. It works 100%

My current challenge :
I'm not sure how to code the 'unshuffle' function.
The script must be able to unshuffle a previously shuffled string back to it's exact original representation, even if the browser cache has been cleared.

Is this possible?

lancemarchetti
Автор

I think it is better to use decrement instead of increment to make sure that the element are shuffled for every loop.

Arcanist
Автор

Unfortunately this is an incorrect implementation as it is somewhat biased due to the range of random numbers being from 0 to the gamesIcon.length every iteration.
Instead it would be more accurately if it is: let r = Math.floor(Math.random() * (gameIcons.length - i)) + i

ventsipetrov
join shbcf.ru