It's easy in Python.
>>> import random
>>> random.choice(['red','green','blue'])
'green'
The reason the code you're looking at is so common is that typically, when you're talking about a random variable in statistics, it has a range of [0,1). Think of it as a percent, if you'd like. To make this percent suitable for choosing a random element, you multiply it by the range, allowing the new value to be between [0,RANGE). The Math.floor() makes certain that the number is an integer, since decimals don't make sense when used as indices in an array.
You could easily write a similar function in Javascript using your code, and I'm sure there are plenty of JS utility libraries that include one. Something like
function choose(choices) {
var index = Math.floor(Math.random() * choices.length);
return choices[index];
}
Then you can simply write choose(answers) to get a random color.
It's easy in Python.
>>> import random
>>> random.choice(['red','green','blue'])
'green'
The reason the code you're looking at is so common is that typically, when you're talking about a random variable in statistics, it has a range of [0,1). Think of it as a percent, if you'd like. To make this percent suitable for choosing a random element, you multiply it by the range, allowing the new value to be between [0,RANGE). The Math.floor() makes certain that the number is an integer, since decimals don't make sense when used as indices in an array.
You could easily write a similar function in Javascript using your code, and I'm sure there are plenty of JS utility libraries that include one. Something like
function choose(choices) {
var index = Math.floor(Math.random() * choices.length);
return choices[index];
}
Then you can simply write choose(answers) to get a random color.
Here you go
function randomChoice(arr) {
return arr[Math.floor(arr.length * Math.random())];
}
Videos
It's a simple one-liner:
const randomElement = array[Math.floor(Math.random() * array.length)];
For example:
const months = ["January", "February", "March", "April", "May", "June", "July"];
const random = Math.floor(Math.random() * months.length);
console.log(random, months[random]);
If you've already got underscore or lodash included in your project you can use _.sample.
// will return one item randomly from the array
_.sample(['January', 'February', 'March']);
If you need to get more than one item randomly, you can pass that as a second argument in underscore:
// will return two items randomly from the array using underscore
_.sample(['January', 'February', 'March'], 2);
or use the _.sampleSize method in lodash:
// will return two items randomly from the array using lodash
_.sampleSize(['January', 'February', 'March'], 2);
The Math.random[MDN] function chooses a random value in the interval [0, 1). You can take advantage of this to choose a value randomly.
var chosenValue = Math.random() < 0.5 ? value1 : value2;
Math.round(Math.random()) returns a 0 or a 1, each value just about half the time.
You can use it like a true or false, 'heads' or 'tails', or as a 2 member array index-
['true','false'][Math.round(Math.random())] will return 'true' or 'false'...