我有一个javascript数组,我想根据我指定的数字或字符串进行排序。我希望数组看起来是随机排序的,但是例如,如果输入为“6543”,则数组总是排序相同。Javascript:根据输入的数字对数组进行排序。应该出现随机排序
3
A
回答
2
使用a shuffle function并用a random number generator where you can set your own seed代替Math.random()
。 然后将您的种子设置为您的输入号码。
0
有了这样一个小小的种子和阵列,你需要用我的例子来创作。
正如Alex建议的那样,他提供的网站有一个很好的随机播放功能。我已经将它与另一个函数结合起来以获得种子的ascii值。
你应该强烈考虑改变我的例子来散列输入。否则会有很多碰撞。
下面是代码:
<script>
// http://sharkysoft.com/tutorials/jsa/content/018.html
function ascii_value (c)
{
// restrict input to a single character
c = c . charAt (0);
// loop through all possible ASCII values
var i;
for (i = 0; i < 256; ++ i)
{
// convert i into a 2-digit hex string
var h = i . toString (16);
if (h . length == 1)
h = "0" + h;
// insert a % character into the string
h = "%" + h;
// determine the character represented by the escape code
h = unescape (h);
// if the characters match, we've found the ASCII value
if (h == c)
break;
}
return i;
}
// http://snippets.dzone.com/posts/show/849
shuffle = function(o,seed){ //v1.0
for(var j, x, i = o.length; i; j = parseInt(seed/(o.length * 255) * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
function seedSort (string){
var charList = string.split('');
var seedValue = 0;
for(var i in charList){
seedValue += ascii_value(charList[i]);
}
return seedValue;
}
document.write(shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],seedSort("bob")));
document.write("<br>");
document.write(shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],seedSort("steve")));
document.write("<br>");
document.write(shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],seedSort("david's house")));
document.write("<br>");
document.write(shuffle([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],seedSort("acorn22")));
</script>
这将产生
8,2,3,4,5,6,7,0,9,1
4,9,3,0,5,6,7,8,1,2
8,0,6,1,5,2,7,3,9,4
4,8,3,0,5,6,7,1,9,2
......其中 “出现” 随机。我会建议大种子。
3
Javascript本身不提供此功能 - 其RNG不能播种。人们可以采用不同的方法。这是一个。种子必须大于1(或相同的数组将被返回),并且应该大于数组大小以获得足够的“随机性”。
Array.prototype.deterministicShuffle=function(seed){
// A little error handling, whynot!
if(!seed)
throw new Error("deterministicShuffle: seed not given, or 0");
var temp,j;
for(var i=0; i<this.length; i++){
// Select a "random" position.
j = (seed % (i+1) + i) % this.length;
// Swap the current element with the "random" one.
temp=this[i];
this[i]=this[j];
this[j]=temp;
}
return this;
}
// Try it out, Aaron!
alert([0,1,2,3,4,5,6,7,8,9].deterministicShuffle(6543));
alert([0,1,2,3,4,5,6,7,8,9].deterministicShuffle(6544));
alert([0,1,2,3,4,5,6,7,8,9].deterministicShuffle(6545));
相关问题
- 1. 根据出现次数对数组进行升序排序
- 2. 根据出现次数对数组进行排序
- 3. 根据用户输入对数组列表进行排序?
- 4. 使用插入排序对输入数据进行排序
- 5. 对插入的唯一随机数组进行排序
- 6. 根据常见词的出现对csv数据进行排序
- 7. 如何根据出现的UNIX数量对行进行排序?
- 8. 当数组输入到数组时,对数组进行排序
- 9. 如何使用相同的随机排序对两个数组进行排序
- 10. 对数组进行排序
- 11. 排序随机数组
- 12. 随机数组排序
- 13. 用JavaScript对数组进行排序时出现错误
- 14. 根据另一个数组对数组进行排序,swift
- 15. 如何根据另一个数组对数组进行排序?
- 16. Javascript:对多维数组进行排序
- 17. 3对已排序的数组进行排序。 O(NlogN)实现
- 18. JavaScript - 内部JavaScript排序方法对数字数据进行排序 - True或False?
- 19. 用jquery对console.log中元素的随机数组进行排序
- 20. 对随机生成的数组进行排序
- 21. 使用随机排序顺序打印字体数组排序
- 22. 如何根据对象的属性对数组进行排序?
- 23. 对字符串数组进行排序
- 24. 如何根据输入字符串对数据视图进行排序?
- 25. Java程序的排序随机数组
- 26. 如何按升序对多维数组输出进行排序?
- 27. [Java]根据玩家分数对数组进行排序
- 28. 如何在javascript中对字母数字数组进行排序
- 29. 使用bubblesort对随机数字的动态数组进行排序
- 30. 如何根据键的出现次数对字典进行“排序”?
你有没有考虑过使用某种哈希机制? – 2011-03-20 21:01:15