Predictable Javascript array shuffle

后端 未结 3 1258
后悔当初
后悔当初 2021-01-19 02:27

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 pag

3条回答
  •  借酒劲吻你
    2021-01-19 02:43

    Chance.js worked perfectly. Thank you Billy Moon.

    My Example:

    
    
    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.

提交回复
热议问题