Answers for "array math random return one item"

10

Javascript get random item from array

var colors = ["red","blue","green","yellow"];
var randomColor = colors[Math.floor(Math.random()*colors.length)]; //pluck a random color
Posted by: Guest on July-26-2019
0

javascript get n random elements from array

// Shuffle array
const shuffled = array.sort(() => 0.5 - Math.random());

// Get sub-array of first n elements after shuffled
let selected = shuffled.slice(0, n);
Posted by: Guest on February-07-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language