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.

Answer from Matt Luongo on Stack Overflow
🌐
W3Schools
w3schools.com › js › js_random.asp
JavaScript Random
As you can see from the examples above, it might be a good idea to create a proper random function to use for all random integer purposes. This JavaScript function always returns a random integer between min (included) and max (excluded):
🌐
W3Schools
w3schools.com › jsref › jsref_random.asp
JavaScript Math random() Method
Math.random() is an ECMAScript1 (JavaScript 1997) feature. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
W3Resource
w3resource.com › javascript-exercises › fundamental › javascript-fundamental-exercise-149.php
JavaScript fundamental (ES6 Syntax): Get a random element from an array - w3resource
//#Source https://bit.ly/2neWfJ2 // Define the sample function const sample = arr => arr[Math.floor(Math.random() * arr.length)]; // Select a random element from the array [3, 7, 9, 11] console.log(sample([3, 7, 9, 11])); // Output: (random ...
🌐
W3Schools
w3schoolsua.github.io › js › js_random_en.html
JavaScript Random. Lessons for beginners. W3Schools in English
As you can see from the examples above, it might be a good idea to create a proper random function to use for all random integer purposes. This JavaScript function always returns a random number between min (included) and max (excluded):
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-array-exercise-35.php
JavaScript array: Get a random item from an array - w3resource
July 12, 2025 - JavaScript exercises, practice and solution: Write a JavaScript function to get random items from an array.
🌐
Kirupa
kirupa.com › html5 › picking_random_item_from_array.htm
Picking a Random Item from an Array | kirupa.com
... To kick your array skills into ... right to it. The code for picking a random value from an array looks as follows: let randomValue = myArray[Math.floor(Math.random() * myArray.length)];...
🌐
W3Schools
w3schools.com › python › ref_random_choice.asp
Python Random choice() Method
The choice() method returns a randomly selected element from the specified sequence. The sequence can be a string, a range, a list, a tuple or any other kind of sequence. ... If you want to use W3Schools services as an educational institution, ...
Find elsewhere
🌐
Codecademy
codecademy.com › forum_questions › 516563716441e86752002d53
Computer Choice: Part 1 | Codecademy
var computerChoice = prompt("Do ... = Math.random; if (computerChoice <0.34){ computerChoice = "rock"; }else if(computerChoice <=0.67){ computerChoice = "paper"; }else{ computerChoice = "scissors"; }...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › random
Math.random() - JavaScript - MDN - Mozilla
The Math.random() static method returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-select-a-random-element-from-array-in-javascript
JavaScript - Select a Random Element from JS Array - GeeksforGeeks
Selecting a random element from array in JavaScript is done by accessing an element using random index from the array.
Published   July 12, 2025
🌐
GeeksforGeeks
geeksforgeeks.org › random-choice-picker-using-html-css-and-javascript
Random Choice Picker using HTML CSS and JavaScript | GeeksforGeeks
In the JavaScript file describe methods for creating choices, picking a random choice, highlighting the choices, removing the highlighting from the choices, and showing the final answer.
Published   April 28, 2025
🌐
TutorialsPoint
tutorialspoint.com › how-to-select-a-random-element-from-array-in-javascript
How to Select a Random Element from Array in JavaScript?
November 20, 2024 - Here is a complete example code implementing above mentioned steps to select a random element from array in JavaScript using Math.random() method.
🌐
Coding Artist
codingartistweb.com › home › css › random choice picker
Random Choice Picker | Coding Artist
October 22, 2024 - Learn how to create Random Choice Picker using HTML, CSS and JS. Download source code or watch video tutorial on my YouTube channel.
🌐
Blobfolio
blobfolio.com › 2019 › randomizing-weighted-choices-in-javascript
Randomizing Weighted Choices in Javascript — Blobfolio
October 19, 2019 - The Math.random() function returns a random float between 0.0 and 1.0. The lower boundary is inclusive, the upper boundary is exclusive (e.g. it will never return 1.0). By multiplying this against the length of the array, then flooring the value to make sure we wind up with a proper integer, ...
🌐
CSS-Tricks
css-tricks.com › snippets › javascript › select-random-item-array
Select Random Item from an Array | CSS-Tricks
December 23, 2016 - JS Result EDIT ON function randomNum(minVal, maxVal) { do { r = Math.random(); } while (r == 1); return minVal+Math.floor(r*(maxVal+1-minVal)); } var coolwords = new Array(); coolwords[0] = “robot”; coolwords[1] = “inferno”; coolwords[2] = “giga”; coolwords[3] = “infinity”; coolwords[4] = “pow”; coolwords[5] = “smash”; coolwords[6] = “boom”; coolwords[7] = “crunch”; coolwords[8] = “robot”; coolwords[9] = “inferno”;
🌐
W3Schools
w3schools.com › js › tryit.asp
W3Schools online HTML editor
The W3Schools online code editor allows you to edit code and view the result in your browser