Discussions

javascript - How to get a number of random elements from an array? - Stack Overflow
I am working on 'how to access elements randomly from an array in javascript'. I found many links regarding this. Like: Get random item from JavaScript array More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
How to select multiple elements randomly from an array (without selecting the same thing twice)?
You could sort the array randomly and just pick the first three elements. (Thats not the most "performant" solution) More on reddit.com
๐ŸŒ r/learnjavascript
5
1
January 14, 2021
Getting a random value from a JavaScript array - Stack Overflow
For a simple problem like this, adding a dependency for an entire library is unnecessary and adds to code bloat. If anything, you could potentially recommend the actual method from Faker which selects a random array element. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-select-a-random-element-from-array-in-javascript
JavaScript - Select a Random Element from JS Array - GeeksforGeeks
How to select a random element from an array in JavaScript? The Math.random() method is used to get the random number between 0 to 1 (1 exclusive).
Published ย  July 12, 2025
๐ŸŒ
W3Resource
w3resource.com โ€บ javascript-exercises โ€บ javascript-array-exercise-35.php
JavaScript array: Get a random item from an array - w3resource
July 12, 2025 - See the Pen JavaScript - Get a random item from an array - array-ex- 35 by w3resource (@w3resource) on CodePen. ... Write a JavaScript function that returns a random element from an array using Math.random() and floor.
๐ŸŒ
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 โ€ฆ
๐ŸŒ
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 - Or when highlighting one of your productโ€™s great features. This tutorial shows you how to grab a random array item in JavaScript. ... You can access every JavaScript array item via its index. For example, get the first item using array[0], the second item using array[1], and so on.
Find elsewhere
๐ŸŒ
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 ...
๐ŸŒ
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?

๐ŸŒ
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 - The implementation below creates ... 2, 3] Getting a random element from an array is as simple as generating a random index and returning the element at that index....
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-get-a-random-item-from-a-javascript-array.php
How to Get a Random Item from a JavaScript Array
You can simply use the Math.random() method in combination with the Math.floor() method to get a random item or value from an array in JavaScript.
๐ŸŒ
Sling Academy
slingacademy.com โ€บ article โ€บ javascript-ways-to-get-a-random-element-from-an-array
JavaScript: 2 Ways to Get a Random Element from an Array - Sling Academy
February 19, 2023 - const arr = ['dog', 'cat', 'dragon','slingacademy.com', 'banana']; // shuffle the array const shuffled = arr.sort(() => 0.5 - Math.random()); // Get the first element from the shuffled array const randomElement = shuffled[0]; console.log(randomElement); ... Due to the randomness, your output ...
๐ŸŒ
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 getRandomElement function takes an array as a parameter and returns a random element 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
Using just a single line of code, learn how to randomly pick an item from an array.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Get a Random Array Item with JavaScript ! - YouTube
In this video, you will learn a JavaScript function, that will return random elements from array.๐Ÿ”” Subscribe for more videos like this : https://www.youtube...
Published ย  May 9, 2022
๐ŸŒ
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 - To select a random element from array in JavaScript, we have used Math.random() with Math.floor() method which picks a random number and rounds down to nearest whole number. We have used a button Select which triggers the function randomElement() upon clicking over it.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ javascript โ€บ select a random element from an array in javascript
How to Select a Random Element From an Array in JavaScript | Delft Stack
February 2, 2024 - We can use the following ways to select a random element from an array in JavaScript: Math.random(), array.length, and Math.floor() together. Use .sample() Method of Lodash and Underscore.js.