1

let 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.

ubuntu7788
  • 115
  • 2
  • 10
  • I am not sure what you are trying to do here. Can you not just remove the number from the array once generated? Or, You want to make sure that the same random number is never generated consecutively. – Runcorn Mar 31 '20 at 06:05
  • Yes, removing is right, I think. How do i remove? I want to make sure that the same number never repeats at all. – ubuntu7788 Mar 31 '20 at 06:07
  • 1
    Possibly Duplicate [How can I shuffle an array?](https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array) – apple apple Mar 31 '20 at 06:17
  • I'm vote to close as dupe sine what @ubuntu7788 want is actually a shuffle and already have answer – apple apple Mar 31 '20 at 06:18
  • note: you can combine the answer with `generator function` for lazy evaluation – apple apple Mar 31 '20 at 06:20

4 Answers4

0

Solution 1 - Check if duplicate

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

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);
Community
  • 1
  • 1
Victor
  • 2,864
  • 1
  • 12
  • 20
  • Splicing is a costly operation (all elements following the chosen one must be slided by one position). You can just instead pick last element and place where the removed element was. – 6502 Mar 31 '20 at 06:46
  • You could say the same for `indexOf` and generating a big pool of numbers, but that's not the point. The goal of the two solutions is not to demonstrate one is faster or more efficient than the other (which really depends on the values of the variables), but that one is more deterministic than the others. – Victor Mar 31 '20 at 10:07
0

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);
Umair Khan
  • 1,684
  • 18
  • 34
0

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

Aldimir
  • 193
  • 1
  • 2
  • 16
0

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.

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:

 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;
             }
         }
     };
 }
6502
  • 112,025
  • 15
  • 165
  • 265