如何从数组中获取随机元素[重复]

时间:2020-12-16 13:45:28

Possible Duplicate:
JavaScript: Getting random value from an array

可能重复:JavaScript:从数组中获取随机值

var numbers = new Array('1','2','4','5','6','7','8','9','10');

I have a JavaScript Array and now want to randomly choose four different numbers from it and then express it on the page (through document.write). Obviously each time the page is reloaded by the user it would show four different random numbers.

我有一个JavaScript数组,现在想从中随机选择四个不同的数字,然后在页面上表达它(通过document.write)。显然,每次用户重新加载页面时,它都会显示四个不同的随机数。

4 个解决方案

#1


51  

You could shuffle the array and pick the first four.

您可以随机播放阵列并选择前四个阵容。

numbers.sort( function() { return 0.5 - Math.random() } );

Now numbers[0], numbers[1]... and so on have random and unique elements.

现在数字[0],数字[1] ......等等具有随机和唯一的元素。

Note that this method may not be an optimal way to shuffle an array: see Is it correct to use JavaScript Array.sort() method for shuffling? for discussion.

请注意,此方法可能不是对数组进行混洗的最佳方法:请参阅使用JavaScript Array.sort()方法进行混洗是否正确?讨论。

#2


19  

If you want this to be as random as the native implementations of JavaScript's Math.random() will allow, you could use something like the following, which also has the advantages of leaving the original array untouched and only randomizing as much of the array as required:

如果你想让它像JavaScript的Math.random()允许的原生实现一样随机,你可以使用类似下面的东西,这样做的好处是保持原始数组不受影响,只将随机化的数组随机化为需要:

function getRandomArrayElements(arr, count) {
    var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
    while (i-- > min) {
        index = Math.floor((i + 1) * Math.random());
        temp = shuffled[index];
        shuffled[index] = shuffled[i];
        shuffled[i] = temp;
    }
    return shuffled.slice(min);
}


var numbers = ['1','2','4','5','6','7','8','9','10'];
alert( getRandomArrayElements(numbers, 4) );

#3


3  

//return a random integer between 0 and 10

document.write(Math.floor(Math.random()*11));

#4


2  

var numbers = new Array('1','2','4','5','6','7','8','9','10');
document.write(numbers[Math.floor(Math.random()*numbers.length)]);

#1


51  

You could shuffle the array and pick the first four.

您可以随机播放阵列并选择前四个阵容。

numbers.sort( function() { return 0.5 - Math.random() } );

Now numbers[0], numbers[1]... and so on have random and unique elements.

现在数字[0],数字[1] ......等等具有随机和唯一的元素。

Note that this method may not be an optimal way to shuffle an array: see Is it correct to use JavaScript Array.sort() method for shuffling? for discussion.

请注意,此方法可能不是对数组进行混洗的最佳方法:请参阅使用JavaScript Array.sort()方法进行混洗是否正确?讨论。

#2


19  

If you want this to be as random as the native implementations of JavaScript's Math.random() will allow, you could use something like the following, which also has the advantages of leaving the original array untouched and only randomizing as much of the array as required:

如果你想让它像JavaScript的Math.random()允许的原生实现一样随机,你可以使用类似下面的东西,这样做的好处是保持原始数组不受影响,只将随机化的数组随机化为需要:

function getRandomArrayElements(arr, count) {
    var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
    while (i-- > min) {
        index = Math.floor((i + 1) * Math.random());
        temp = shuffled[index];
        shuffled[index] = shuffled[i];
        shuffled[i] = temp;
    }
    return shuffled.slice(min);
}


var numbers = ['1','2','4','5','6','7','8','9','10'];
alert( getRandomArrayElements(numbers, 4) );

#3


3  

//return a random integer between 0 and 10

document.write(Math.floor(Math.random()*11));

#4


2  

var numbers = new Array('1','2','4','5','6','7','8','9','10');
document.write(numbers[Math.floor(Math.random()*numbers.length)]);