Answers for "create an array of random numbers javascript"

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
9

how to get a random element of an array javascript

var foodItems = ["Bannana", "Apple", "Orange"];
var theFood = foodItems[Math.floor(Math.random() * foodItems.length)];
/* Will pick a random number from the length of the array and will go to the
corosponding number in the array E.G: 0 = Bannana */
Posted by: Guest on May-30-2020
1

js get random from array

//Make all arrays have "random" method
Array.prototype.random = function() {
    return this[Math.floor(Math.random() * this.length)];
}

//Call "random" method on an array
var result = ["Hello", "world"].random();
Posted by: Guest on March-27-2021
1

generate random number array javascript from principal array

let numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
    generated = [];
for (let i = 0; i < 10; i++)
{
    let random_index;
    while(!random_index)
    {
        let tmp = Math.floor(Math.random() * numbers.length);        
        if( !generated.filter( (g) => numbers[tmp] == g).length )
            random_index = tmp;
    }
    generated.push(numbers[random_index]);
}
console.log(generated);
Posted by: Guest on August-08-2020
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
0

how to get a random statement from an array in javascript

// List your array items
let Testing1 = ["put","things","here"]
let Generate = Math.floor((Math.random() * Testing1.length)); // Generates a number of the array.

// logs the result
console.log(Testing1[Generate])
Posted by: Guest on September-11-2020

Code answers related to "create an array of random numbers javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language