我正在研究如何在JavaScript中随机访问数组元素。我发现了许多相关链接,比如: 从JavaScript数组获取随机项
var item = items[Math.floor(Math.random()*items.length)];
但是在这种情况下,我们只能从数组中选择一个项目。如果我们想要多个元素,该怎么办?如何从数组中获取多个元素?
我正在研究如何在JavaScript中随机访问数组元素。我发现了许多相关链接,比如: 从JavaScript数组获取随机项
var item = items[Math.floor(Math.random()*items.length)];
但是在这种情况下,我们只能从数组中选择一个项目。如果我们想要多个元素,该怎么办?如何从数组中获取多个元素?
它从srcArray中逐个提取随机元素,直到获取足够的元素或者srcArray中没有更多元素可供提取。 快速而可靠。
function getNRandomValuesFromArray(srcArr, n) {
// making copy to do not affect original srcArray
srcArr = srcArr.slice();
resultArr = [];
// while srcArray isn't empty AND we didn't enough random elements
while (srcArr.length && resultArr.length < n) {
// remove one element from random position and add this element to the result array
resultArr = resultArr.concat( // merge arrays
srcArr.splice( // extract one random element
Math.floor(Math.random() * srcArr.length),
1
)
);
}
return resultArr;
}
实际上你不需要排序,你只需要生成一个随机的子数组长度:
const myArray = ["January", "February", "March", "April", "May", "June", "July"];
const randomNumGenerator = () => Math.floor(Math.random() * myArray.length)
const result = [
...new Set(
Array.from({
length: randomNumGenerator() + 1
},
_ => myArray[randomNumGenerator()])
)
]
console.log(result)
// Returns a random sample (either with or without replacement) from an array
const randomSample = (arr, k, withReplacement = false) => {
let sample;
if (withReplacement === true) { // sample with replacement
sample = Array.from({length: k}, () => arr[Math.floor(Math.random() * arr.length)]);
} else { // sample without replacement
if (k > arr.length) {
throw new RangeError('Sample size must be less than or equal to array length when sampling without replacement.')
}
sample = arr.map(a => [a, Math.random()]).sort((a, b) => {
return a[1] < b[1] ? -1 : 1;}).slice(0, k).map(a => a[0]);
};
return sample;
};
使用它很简单:
无替换(默认行为)
randomSample([1, 2, 3], 2)
可能返回 [2, 1]
有替换
randomSample([1, 2, 3, 4, 5, 6], 4)
可能返回 [2, 3, 3, 2]
var getRandomElements = function(sourceArray, requiredLength) {
var result = [];
while(result.length<requiredLength){
random = Math.floor(Math.random()*sourceArray.length);
if(result.indexOf(sourceArray[random])==-1){
result.push(sourceArray[random]);
}
}
return result;
}
items.sort(() => (Math.random() > 0.5 ? 1 : -1)).slice(0, count);
将数组 items 随机排序并截取前 count 个元素。
这里是最正确的答案,它将为您提供随机+独特的元素。
function randomize(array, n)
{
var final = [];
array = array.filter(function(elem, index, self) {
return index == self.indexOf(elem);
}).sort(function() { return 0.5 - Math.random() });
var len = array.length,
n = n > len ? len : n;
for(var i = 0; i < n; i ++)
{
final[i] = array[i];
}
return final;
}
// randomize([1,2,3,4,5,3,2], 4);
// Result: [1, 2, 3, 5] // Something like this