Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Predictable Javascript array shuffle [duplicate]

I'm trying to predictably shuffle javascript arrays the same way each time the webpage is loaded.

I can shuffle the arrays randomly, but every time i reload the page it's a different sequence.

I'd like it to shuffle the arrays the same way every time the page loads. There are many arrays and they are part of a procedurally generated world.

like image 747
Protopop Avatar asked Jan 31 '15 21:01

Protopop


1 Answers

Chance.js worked perfectly. Thank you Billy Moon.

My Example:

<script type="text/javascript" src="assets/js/chance.js"></script>

var chance1 = new Chance(124); // you can choose a seed here, i chose 124
console.log(chance1.shuffle(['alpha', 'bravo', 'charlie', 'delta', 'echo']));
// Array [ "alpha", "delta", "echo", "charlie", "bravo" ]

As long as you set the seed with new Chance(xxx) you get the same result every time.

like image 149
Protopop Avatar answered Oct 17 '22 00:10

Protopop