1
來自同一陣列中兩個不同的隨機項目,我希望得到來自JS同一陣列中兩個不同的隨機項目。有堆棧溢出相關的問題,但我不明白Fisher Yates Shuffle是如何工作的。我需要搜索整個數組來檢索這些項目,但數組的大小很小。獲取JS
目前,我有一個while循環,但這似乎並沒有被acheiving的最有效的方法:
var honeyPots = ["Fname", "EmailAddress", "Lname", "Telephone", "Address1", "Address2", "Surname", "Title"]; //Fake field names to dupe the bots!
var honeyPot = honeyPots[Math.floor(Math.random()*honeyPots.length)]; //Get a random field name from the array
var honeyPot2 = honeyPots[Math.floor(Math.random()*honeyPots.length)]; //Get a random field name from the array
while (honeyPot == honeyPot2)
{
var honeyPot2 = honeyPots[Math.floor(Math.random()*honeyPots.length)];
}
1爲那種功能 – tewathia
對不起,是迂腐,是那種功能的類似的概念到其中費希爾耶茨隨機定義? – Barney
不,這是另一種方法。 –