特别是,我想确保避免Microsoft的Browser Choice shuffle代码中出现的错误。也就是说,我想确保每封信都有可能在每个可能的位置结束。如何在JavaScript中对字符串中的字符进行混排?
例如给定“ABCDEFG”,返回类似“GEFBDCA”的内容。
特别是,我想确保避免Microsoft的Browser Choice shuffle代码中出现的错误。也就是说,我想确保每封信都有可能在每个可能的位置结束。如何在JavaScript中对字符串中的字符进行混排?
例如给定“ABCDEFG”,返回类似“GEFBDCA”的内容。
我修改从Fisher-Yates Shuffle entry on Wikipedia一个例子洗牌字符串:
String.prototype.shuffle = function() {
var a = this.split(""),
n = a.length;
for(var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
return a.join("");
}
console.log("the quick brown fox jumps over the lazy dog".shuffle());
//-> "veolrm hth ke opynug tusbxq ocrad ofeizwj"
console.log("the quick brown fox jumps over the lazy dog".shuffle());
//-> "o dt hutpe u iqrxj yaenbwoolhsvmkcger ozf "
更多信息可在Jon Skeet's answer被发现Is it correct to use JavaScript Array.sort() method for shuffling?。
如果“真正”的随机性很重要,我建议不要这样做。看到我下面的编辑。
我只是想补充我最喜欢的方法有点不同;)
给定一个字符串:
var str = "My bologna has a first name, it's O S C A R.";
洗牌在一条线:
var shuffled = str.split('').sort(function(){return 0.5-Math.random()}).join('');
输出:
oa, a si'rSRn f gbomi. aylt AtCnhO ass eM
as'oh ngS li Ays.rC nRamsb Oo ait a ,eMtf
y alCOSf e gAointsorasmn bR Ms .' ta ih,a
编辑:正如@PleaseStand指出的,这完全不符合OP的问题,因为它确实遭受“微软浏览器选择洗牌”代码的困扰。如果你的字符串需要接近随机数,这不是一个很好的随机数发生器。然而,它很快就会让你的琴弦“混淆”,其中“真实”的随机性无关紧要。
他在下面链接的文章是一个很好的阅读,但解释了一个完全不同的用例,它影响统计数据。我个人无法想象在字符串上使用这个“随机”功能的实际问题,但作为编码人员,您有责任了解什么时候使用而不是。
我已经在这里留下了这里所有的随机随机数发生器。
-1:这具有确切的“微软浏览器选择洗牌代码中的错误”。 http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html – PleaseStand 2014-08-27 23:40:25
@PleaseStand你绝对正确。我一直都知道这是一个非常懒惰的黑客,实际上并不会产生非常随机的结果。我会更新我的答案,但我不会说这是“确切的错误” - 那是为了调查结果。 – 2014-08-28 17:42:40
String.prototype.shuffle=function(){
var that=this.split("");
var len = that.length,t,i
while(len){
i=Math.random()*len-- |0;
t=that[len],that[len]=that[i],that[i]=t;
}
return that.join("");
}
shuffleString = function(strInput){
var inpArr = strInput.split("");//this will give array of input string
var arrRand = []; //this will give shuffled array
var arrTempInd = []; // to store shuffled indexes
var max = inpArr.length;
var min = 0;
var tempInd;
var i =0 ;
do{
tempInd = Math.floor(Math.random() * (max - min));//to generate random index between range
if(arrTempInd.indexOf(tempInd)<0){ //to check if index is already available in array to avoid repeatation
arrRand[i] = inpArr[tempInd]; // to push character at random index
arrTempInd.push(tempInd); //to push random indexes
i++;
}
}
while(arrTempInd.length < max){ // to check if random array lenght is equal to input string lenght
return arrRand.join("").toString(); // this will return shuffled string
}
};
只是把这个字符串的功能和得到的回报的洗后串
添加示例如何通过。 – 2016-07-21 12:54:45
尽管这已经回答了,我想和大家分享我想出了一个解决方案:
function shuffelWord (word){
var shuffledWord = '';
word = word.split('');
while (word.length > 0) {
shuffledWord += word.splice(word.length * Math.random() << 0, 1);
}
return shuffledWord;
}
// 'Batman' => 'aBmnta'
谢谢,这肯定比我发现的其他一些例子更统一。 – Liam 2010-10-15 16:23:53