samedi 19 juin 2021

How to shuffle an array using a seed whereby the sequence of elements remains consistent when additional items are added to the array

Note: This question can apply to any programming language, for example Python or JavaScript.


How would you shuffle an array of elements deterministically with a seed, but where the following is also guaranteed:

  • If you add an additional element to the array before shuffling, the sequence of the original elements remains the same as with shuffling the original array.

I can probably explain this better with an example:

  • Let's say the array [a, b, c] is shuffled with seed 123, and this results in the output [c, a, b].
  • As you can see, b comes after a, and a comes after c.
  • We add an additional element to the end of the array, [a, b, c, d], and proceed to shuffle with seed 123.
  • This time, b must still come after a, and a must still come after c.
  • The output might be [c, a, d, b] or [d, c, a, b], but cannot be [b, a, c, d].
  • The same must apply if we continue to add more elements.



Aucun commentaire:

Enregistrer un commentaire