Answers for "math.random javascript array"

49

javascript get random array value

//get random value from array
var colors = ["red","blue","green","yellow"];
var randColor = colors[Math.floor(Math.random() * colors.length)];
Posted by: Guest on July-31-2019
-1

random array javascript

const months = ["January", "February", "March", "April", "May", "June", "July"];

const random = Math.floor(Math.random() * months.length);
console.log(random, months[random]);
Posted by: Guest on February-22-2021
3

javascript generate random array

//This example is to generate an array with 40 random elements, with random values from 0 to 39 

for (var a=[],i=0;i<40;++i) a[i]=i;

// http://stackoverflow.com/questions/962802#962890
function shuffle(array) {
  var tmp, current, top = array.length;
  if(top) while(--top) {
    current = Math.floor(Math.random() * (top + 1));
    tmp = array[current];
    array[current] = array[top];
    array[top] = tmp;
  }
  return array;
}

a = shuffle(a);
Posted by: Guest on January-30-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language