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.
Videos
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())];
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
var computerResponse = getRandomInt(1, 3);
Taken from MDN's documentation on Math.random().
You can go with this below javascriptscript code that would be fetched random numbers...
var randomnumber = Math.floor(Math.random()*3)
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);