How to select random values ​​from an array in javascript or jquery?

I am trying to show 3 random values ​​from an array. After the script, only one element is returned from the javaScript array.

var arrayNum = ['One', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];  
var singleRandom = arrayNum[Math.floor(Math.random() * arrayNum.length)];
alert(singleRandom);

But I want to show three random values ​​from an array arrayNum, can anyone guide me on whether it is possible to get 3 unique random values ​​from an array using javascript? I would be grateful if someone would lead me. Thanks you

+4
source share
4 answers

I'm going to assume that you are asking how to get a new array of three elements in your current array.

If you do not agree with the possible duplication, you can do something simple: getThreebelow.

, , getUnique.

var arrayNum = ['One', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];  

function getThree() {
  return  [
    arrayNum[Math.floor(Math.random() * arrayNum.length)],
    arrayNum[Math.floor(Math.random() * arrayNum.length)],
    arrayNum[Math.floor(Math.random() * arrayNum.length)]
  ];
    
}


function getUnique(count) {
  // Make a copy of the array
  var tmp = arrayNum.slice(arrayNum);
  var ret = [];
  
  for (var i = 0; i < count; i++) {
    var index = Math.floor(Math.random() * tmp.length);
    var removed = tmp.splice(index, 1);
    // Since we are only removing one element
    ret.push(removed[0]);
  }
  return ret;  
}
console.log(getThree());

console.log("---");
console.log(getUnique(3));
Hide result
+6

- :

:

  • , .
  • % array.length, .
  • array.splice(index, 1), temp, .

var arrayNum = ['One', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];  

function getRandomValues(arr, count){
  var result = [];
  var _tmp = arr.slice();
  for(var i = 0; i<count; i++){
    var index = Math.ceil(Math.random() * 10) % _tmp.length;
    result.push(_tmp.splice(index, 1)[0]);
  }
  return result;
}

console.log(getRandomValues(arrayNum, 3))
Hide result
+2

for()

var arrayNum = ['One', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];  
var selected = [];
for (var i = 0; i < 3; i++){
    selected[i] = arrayNum[Math.floor(Math.random() * arrayNum.length)];
}
console.log(selected);
Hide result

, ​​ .

var arrayNum = ['One', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];  
var selected = [];
for (var i = 0; i < 3; i++){
    rand();
}
console.log(selected);

function rand(){
    var ran = arrayNum[Math.floor(Math.random() * arrayNum.length)];  
    if (selected.indexOf(ran) == -1)
        selected.push(ran);
    else
         rand();
}
Hide result
+1

- :

var arrayNum = ['One', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];

var singleRandom = [];

for (var i = 0; i < 3; i++) {

  singleRandom.push(Math.floor(Math.random() * arrayNum.length));

}

console.log(arrayNum[singleRandom[0]]);
console.log(arrayNum[singleRandom[1]]);
console.log(arrayNum[singleRandom[2]]);
Hide result
0

All Articles