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
🌐
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
🌐
W3Schools
w3schools.com › js › js_random.asp
JavaScript Random
Math.random() returns a number from 0 (inclusive) up to but not including 1.
🌐
GeeksforGeeks
geeksforgeeks.org › 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   January 9, 2025
🌐
Kirupa
kirupa.com › html5 › picking_random_item_from_array.htm
Picking a Random Item from an Array | kirupa.com
Using just a single line of code, learn how to randomly pick an item from an array.
🌐
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, ...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Struggling to make a random option selector (HTML, Javascript) - JavaScript - The freeCodeCamp Forum
April 5, 2019 - Hi all, I’m trying to make a randomised selector for user inputted options in an html form (e.g. user cn put in “go to the cinema”, “go bowling”, “go to the beach” as options and javascript would choose one at random and then alert) But after days of trying to work out what I’m ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › 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   July 23, 2025
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-array-exercise-35.php
JavaScript array: Get a random item from an array - w3resource
July 12, 2025 - Write a JavaScript function that shuffles an array and returns its first element as a random item.
🌐
p5.js
p5js.org › reference › p5 › random
random
When random() is used to select elements from an array, all elements are equally likely to be chosen.
🌐
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”;
🌐
Codecademy
codecademy.com › forum_questions › 516563716441e86752002d53
Computer Choice: Part 1 | Codecademy
var computerChoice = prompt("Do you choose Rock, Paper, or Scissors?"); var computerChoice = Math.random; if (computerChoice <0.34){ computerChoice = "rock"; }else if(computerChoice <=0.67){ computerChoice = "paper"; }else{ computerChoice = "scissors"; }
🌐
Vultr Docs
docs.vultr.com › javascript › examples › get-random-item-from-an-array
JavaScript Program to Get Random Item From an Array | Vultr Docs
December 18, 2024 - The succinct ES6 version of getRandomItem easily handles arrays containing various data types, fetching a random element each time. Utilizing JavaScript to select a random item from an array is a useful skill that can be applied in numerous programming scenarios, from game development to data ...
🌐
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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › random
Math.random() - JavaScript - MDN Web Docs
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 ...
🌐
Readthedocs
labjs.readthedocs.io › en › latest › reference › random.html
Random data generation — lab.js 23.0.0-alpha2 documentation
this.options.parameters['greeting'] = this.random.choice(['aloha', 'as-salamualaikum', 'shalom', 'namaste']) This will select one of the greetings at random, and save it in the greeting parameter. The value is then available for re-use whereever parameters can be inserted, and will be included in the dataset.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › array › sample, shuffle and weighted selection
Sampling, shuffling and weighted selection in JavaScript arrays - 30 seconds of code
March 15, 2024 - const weightedSample = (arr, weights) => { let roll = Math.random(); return arr[ weights .reduce( (acc, w, i) => (i === 0 ? [w] : [...acc, acc[acc.length - 1] + w]), [] ) .findIndex((v, i, s) => roll >= (i === 0 ? 0 : s[i - 1]) && roll < v) ]; }; weightedSample([3, 7, 9, 11], [0.1, 0.2, 0.6, 0.1]); // 9 ... Learn how to work with arrays of numbers in JavaScript, performing common math operations such as sum, average, product and more.