0

完了できない厄介なスクリプトがあります。

可能な 0 ~ 64 の数字セットから 32 個の非反復数字が必要です。配列内のすべての数値に対して新しい乱数をチェックするループを作成しようとするたびに、何もないか、無限ループになります。

私は困惑しています:(

4

2 に答える 2

3

これを試して:

var keys = [], numbers = [], x, total = 0;
while(total < 32) {
    x = Math.floor(Math.random() * 64);
    // way faster that looping through the array to check if it exists
    if(keys[x] == undefined) { 
        keys[x] = 1;
        numbers.push(x);
        total++;
    }
}
console.log(numbers);

ワーキングデモ

于 2013-05-24T06:09:55.110 に答える