predictable JavaScript数组洗牌 [英] Predictable Javascript array shuffle
本文介绍了predictable JavaScript数组洗牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想$ P $每个网页加载时间pdictably洗牌JavaScript数组的方式相同。
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.
推荐答案
Chance.js完美工作。谢谢比利月亮。
Chance.js worked perfectly. Thank you Billy Moon.
我的例子:
<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" ]
只要你设定新的机遇(XXX)你每次都相同的结果的种子。
As long as you set the seed with new Chance(xxx) you get the same result every time.
这篇关于predictable JavaScript数组洗牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文