简体   繁体   English

如何从 JavaScript 中的数字数组生成非重复随机数?

[英]How do I generate a non-repeating random from an array of number in JavaScript?

let arr = [2,3,5,7,9] arr[Math.floor(Math.random() * arr.length)]让 arr = [2,3,5,7,9] arr[Math.floor(Math.random() * arr.length)]

arr[Math.floor(Math.random() * arr.length)] //5
arr[Math.floor(Math.random() * arr.length)] //2
arr[Math.floor(Math.random() * arr.length)] //9
arr[Math.floor(Math.random() * arr.length)] //9

9 is repeating here, how do i make sure that the random number never repeats. 9 在这里重复,我如何确保随机数永远不会重复。

Solution 1 - Check if duplicate解决方案 1 - 检查是否重复

var min_number = 0;
var max_number = 100;
var numbers_to_generate = 10; 
// Make sure numbers_to_generate < (max_number - min_number + 1);

var numbers = [];
while(numbers.length < numbers_to_generate){
    var number = Math.floor(Math.random() * (max_number - min_number)) + min_number;
    if(numbers.indexOf(number) === -1) {
        numbers.push(number);
    }
}
console.log(numbers);

Solution 2 - Randomly get from a pool of numbers解决方案 2 - 从数字池中随机获取

var min_number = 0;
var max_number = 100;
var numbers_to_generate = 10; 
// Make sure numbers_to_generate < (max_number - min_number)

var numbers = [];
var pool = Array.from(new Array(max_number-min_number), (x,i) => i + min_number);
while(numbers.length < numbers_to_generate){
    var number = pool.splice(Math.floor(Math.random() * (pool.length-1)), 1)[0];
    numbers.push(number);
}
console.log(numbers);

Some tricks / ways to remove duplicates from array.从数组中删除重复项的一些技巧/方法。

 let arr = [1, 1, 2, 3, 3, 4, 5, 5, 6]; let uArr1 = [...new Set(arr)]; console.log(uArr1); let uArr2 = arr.filter((val, ind) => arr.indexOf(val) === ind); console.log(uArr2);

Is this what you're asking for?这是你要的吗?

 let arr = [2,3,5,7,9] console.log("This is your arr " + arr) let unique = []; let numbers_to_generate = arr.length; let index; let i = 0; while(unique.length != numbers_to_generate){ unique.push(arr[Math.floor(Math.random() * arr.length)]); index = arr.indexOf(unique[i]); arr.splice(index, 1) i++ } console.log("This is unique " + unique)

After this you just have to get the first element of unique, I dont really know what you want to do在这之后你只需要得到唯一的第一个元素,我真的不知道你想做什么

If the elements in the array are unique and you need many (or all) of them, a simple approach is: 1) shuffle the array, 2) pick first n elements.如果数组中的元素是唯一的,并且您需要很多(或全部)元素,一个简单的方法是:1) 对数组进行洗牌,2) 选取前n元素。

If you don't need many of them shuffling the whole array is overkilling and picking can instead be intermixed with the shuffling:如果您不需要其中的许多人洗牌,则整个阵列是矫枉过正的,而采摘可以与洗牌混合:

 function randomOrder(L) {
     let data = L.slice(), count = L.length;
     return function() {
         if (count === 0) throw Error("No more values available");
         let j = Math.floor(Math.random()*count);
         let value = L[j];
         L[j] = L[--count];
         return value;
     };
 }

If elements are not unique you must also do a check that the same value was not used before;如果元素不是唯一的,您还必须检查以前没有使用过相同的值; depending on the size of the array and on how many elements you want to extract it could make sense to use a Set object to track them:根据数组的大小以及要提取的元素数量,使用Set对象来跟踪它们可能是有意义的:

 function randomOrderDistinct(L) {
     let data = L.slice(), count = L.length, used = new Set;
     return function() {
         for(;;) {
             if (count === 0) throw Error("No more values available");
             let j = Math.floor(Math.random()*count);
             let value = L[j];
             L[j] = L[--count];
             if (!used.has(value)) {
                 used.add(value);
                 return value;
             }
         }
     };
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM