1//get random value from array
2var colors = ["red","blue","green","yellow"];
3var randColor = colors[Math.floor(Math.random() * colors.length)];
4
1var items = ['Yes', 'No', 'Maybe'];
2var item = items[Math.floor(Math.random() * items.length)];
1var colors = ["red","blue","green","yellow"];
2var randomColor = colors[Math.floor(Math.random()*colors.length)]; //pluck a random color
3
1// how to generate random words from an array
2const Coins = ["Heads","Tails"]
3let Generate = Math.floor((Math.random() * Coins.length));
4
5console.log(Coins[Generate]) // this will print the outcome
1//This example is to generate an array with 40 random elements, with random values from 0 to 39
2
3for (var a=[],i=0;i<40;++i) a[i]=i;
4
5// http://stackoverflow.com/questions/962802#962890
6function shuffle(array) {
7 var tmp, current, top = array.length;
8 if(top) while(--top) {
9 current = Math.floor(Math.random() * (top + 1));
10 tmp = array[current];
11 array[current] = array[top];
12 array[top] = tmp;
13 }
14 return array;
15}
16
17a = shuffle(a);
18