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]);

Answer from Jacob Relkin on Stack Overflow
🌐
Reddit
reddit.com › r/learnjavascript › how to select multiple elements randomly from an array (without selecting the same thing twice)?
r/learnjavascript on Reddit: How to select multiple elements randomly from an array (without selecting the same thing twice)?
January 14, 2021 -

EDIT BELOW

I created an array consisting of foods, for example 'apple', 'banana', 'orange', 'cherry' etc. Now I need something to randomly select 3 elements from this array, without selecting the same thing twice (for example not 'orange' 'orange' 'banana'). I have searched for a solution myself, but I'm fairly new to coding and don't really understand everything. Can someone help me a little bit?

EDIT: I think I found a way using

const randomSelectionH = (n) => {
  let newArr = [];
  if (n >= healthy_foods.length) {
    return healthy_foods;
  }
  for (let i = 0; i < n; i++) {
    let newElem = healthy_foods[Math.floor(Math.random() * healthy_foods.length)];
    while (newArr.includes(newElem)) {
      newElem = healthy_foods[Math.floor(Math.random() * healthy_foods.length)];
    }
    newArr.push(newElem);
  }
  return newArr;
}

But now, I need to use this to put the random selection into a html-button-response

I used:

var trial_two_choices = {
  type: 'html-button-response',
  stimulus: '<p>What food</p>',
  choices: [randomSelectionH]
}

But this returned an error. trying to make a variable out of the randomSelectionH and putting that as choices didn't resolve the issue. Any idea on what to do?

Discussions

javascript - How to get a number of random elements from an array? - Stack Overflow
It then sorts the array of pairs ... Finally, it picks the first n items of the (randomly ordered) array of items (line 6). For a better understanding, read the documentation of functions like map and sort, like in developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. ... More on stackoverflow.com
🌐 stackoverflow.com
Pick two random items from an array
Hello everyone, I’m currently failing to select two random items from an array. I select a random item like this: {{get(2.customArray; floor(random * length(2.customArray)) + 1)}} What is the right approach now?Delete the selected item from the array and pick a new one? More on community.make.com
🌐 community.make.com
1
0
August 9, 2024
Selecting a random element from a JavaScript array
Hey everyone! I’m working on a project where I need to pick a random element from an array in JavaScript. I’ve got an array with a bunch of numbers like this: let numberList = [100, 42, 789, 13, 555, 27, 1001]; What’s the best way to grab a random item from this array? More on community.latenode.com
🌐 community.latenode.com
0
0
March 11, 2025
Selecting a random object from an array
I have tried to select random thing from an array but it doesn’t work. Any advice? function randomRange(myMin, myMax) { return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin; } var colours = [ [“blue”, “green”, “red”], ] var i = randomRange(1,3); function randomColours() ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
February 4, 2023
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-get-multiple-random-elements-from-array
Get one or Multiple Random Elements from an Array in JS | bobbyhadz
March 3, 2024 - The sampleSize method takes an array and n as parameters and returns n random elements from the array. Note: If you need to get a random property from an object, click on the following subheading: Get a Random Property from an Object in JavaScript
🌐
Kirupa
kirupa.com › html5 › picking_random_item_from_array.htm
Picking a Random Item from an Array | kirupa.com
In this article, we will learn how to write this JavaScript ourselves. We will learn how to write some code to randomly pick an item from array filled with items!
🌐
TutorialsPoint
tutorialspoint.com › javascript-how-to-pick-random-elements-from-an-array
JavaScript - How to pick random elements from an array?
September 30, 2020 - We are required to write a JavaScript function that takes in an array of unique literals and a number n. The function should return an array of n elements all chosen randomly from the input array and no element should appear more than once in the output array.
Find elsewhere
🌐
Latenode
community.latenode.com › other questions › javascript
Selecting a random element from a JavaScript array - JavaScript - Latenode Official Community
March 11, 2025 - Hey everyone! I’m working on a project where I need to pick a random element from an array in JavaScript. I’ve got an array with a bunch of numbers like this: let numberList = [100, 42, 789, 13, 555, 27, 1001]; What’s …
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-select-a-random-element-from-array-in-javascript
JavaScript – Select a Random Element from JS Array | GeeksforGeeks
The Math.random() method is used to get the random number between 0 to 1 (1 exclusive). It can be multiplied with the size of the array to get a random index and access the respective element using their index.
Published   July 9, 2025
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-array-exercise-35.php
JavaScript array: Get a random item from an array - w3resource
July 12, 2025 - ... // Function to return a random item from an array function random_item(items) { // Use Math.random() to generate a random number between 0 and 1, // multiply it by the length of the array, and use Math.floor() to round down to the nearest ...
🌐
Dirask
dirask.com › posts › JavaScript-get-multiple-random-elements-from-array-D9WM0j
JavaScript - get multiple random elements from array
April 12, 2025 - ... // ONLINE-RUNNER:browser; const array = ['a', 'b', 'c', 'd']; const n = 2; // number of elements we want to get const shuffledArray = array.sort(() => 0.5 - Math.random()); // shuffles array const result = shuffledArray.slice(0, n); // gets ...
🌐
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 - Use the refactored getRandomItem function to pick a random element. ... const elements = [10, 'Hello', true, { key: 'value' }, [1, 2, 3]]; const randomElement = getRandomItem(elements); console.log(randomElement); Explain Code · The array elements ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-select-a-random-element-from-array-in-javascript
JavaScript - Select a Random Element from JS Array - GeeksforGeeks
The Math.random() method is used to get the random number between 0 to 1 (1 exclusive). It can be multiplied with the size of the array to get a random index and access the respective element using their index.
Published   July 12, 2025
🌐
Programiz
programiz.com › javascript › examples › get-random-item
JavaScript Program to Get Random Item From an Array
To understand this example, you should have the knowledge of the following JavaScript programming topics: ... // program to get a random item from an array function getRandomItem(arr) { // get random index value const randomIndex = Math.floor(Math.random() * arr.length); // get random item ...
🌐
Linux Hint
linuxhint.com › select-random-element-from-an-array-javascript
How to Select a Random Element From Array in JavaScript?
April 8, 2025 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Futurestud.io
futurestud.io › tutorials › retrieve-a-random-item-from-an-array-in-javascript-or-node-js
Retrieve a Random Item From an Array in JavaScript or Node.js
June 24, 2021 - You can then multiply the random number by the number of items in the array. The result will likely be a decimal number. Round the result to the nearest integer that is equal to or lower than the random value using Math.floor(). That calculation provides you a random index that you can use ...
🌐
W3Resource
w3resource.com › javascript-exercises › fundamental › javascript-fundamental-exercise-226.php
JavaScript fundamental (ES6 Syntax): Get n random elements at unique keys from array up to the size of array - w3resource
July 9, 2025 - // Define a function 'sampleSize' to randomly sample elements from an array const sampleSize = ([...arr], n = 1) => { let m = arr.length; // Shuffle the array using the Fisher-Yates algorithm while (m) { const i = Math.floor(Math.random() * ...
🌐
IncludeHelp
includehelp.com › code-snippets › how-to-get-multiple-random-unique-elements-from-an-array-in-javascript.aspx
How to get multiple random unique elements from an array in JavaScript?
February 7, 2021 - <html> <body> <div id="one"> <p>one</p> </div> <div id="two"> <p>two</p> </div> <script> function shuffle(a) { for (let i = a.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [a[i], a[j]] = [a[j], a[i]]; } return a; } var arr = [ "<span class=\"booklink\"><a href=\"/one\">one</a></span>", "<span class=\"booklink\"><a href=\"/one\">two</a></span>", "<span class=\"booklink\"><a href=\"/one\">three</a></span>", "<span class=\"booklink\"><a href=\"/one\">four</a></span>", "<span class=\"booklink\"><a href=\"/one\">five</a></span>" ] /* note: the JavaScript that updates the
🌐
David Walsh
davidwalsh.name › javascript-random-array
Get a Random Array Item with JavaScript
May 9, 2022 - To get a random item from an array, you can employ Math.random: const arr = [ "one", "two", "three", "four", "tell", "me", "that", "you", "love", "me", "more" ]; const random1 = arr[(Math.floor(Math.random() * (arr.length)))] const random2 = ...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Selecting a random object from an array - JavaScript - The freeCodeCamp Forum
February 4, 2023 - Any advice? function randomRange(myMin, myMax) { return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin; } var colours = [ [“blue”, “green”, “red”], ] var i = randomRange(1,3); function randomColours() ...