特別是,我想確保避免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