xxxxxxxxxx
function getRandomNumberElement(items: number[]): number {
let randomIndex = Math.floor(Math.random() * items.length);
return items[randomIndex];
}
Code language: TypeScript (typescript)
xxxxxxxxxx
var colors = ["red","blue","green","yellow"];
var randomColor = colors[Math.floor(Math.random()*colors.length)]; //pluck a random color
xxxxxxxxxx
var items = ['Yes', 'No', 'Maybe'];
var item = items[Math.floor(Math.random() * items.length)];
xxxxxxxxxx
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 */
xxxxxxxxxx
function RandomItemFromArray(myArray) {
return myArray[Math.floor(Math.random() * myArray.length)]
}
var fruit = [ "Apples", "Bananas", "Pears", ];
let fruitSample = RandomItemFromArray(fruit)
xxxxxxxxxx
const rnd = (arr) => { return arr[Math.floor(Math.random() * arr.length)] };
xxxxxxxxxx
let items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119];
let randomItem = items[Math.floor(Math.random() * items.length)];
xxxxxxxxxx
const randomValue = (list) => {
return list[Math.floor(Math.random() * list.length)];
};
xxxxxxxxxx
// Array of elements
const array = [1, 2, 3, 4, 5];
// Get random element from array
const randomElement = array[Math.floor(Math.random() * array.length)];
console.log(randomElement);