var item = items[Math.floor(Math.random()*items.length)];
Answer from Kelly on Stack OverflowEducative
educative.io › answers › how-to-get-a-random-element-from-an-array-using-lodash
How to get a random element from an array using Lodash
Line 5: We get a random element from the array using the _.sample() method.
Top answer 1 of 13
3157
var item = items[Math.floor(Math.random()*items.length)];
2 of 13
167
1. solution: define Array prototype
Array.prototype.random = function () {
return this[Math.floor((Math.random()*this.length))];
}
that will work on inline arrays
[2,3,5].random()
and of course predefined arrays
var list = [2,3,5]
list.random()
2. solution: define custom function that accepts list and returns element
function get_random (list) {
return list[Math.floor((Math.random()*list.length))];
}
get_random([2,3,5])
Feature Requests: Pick one or more random entries out of an array, string, or object
Pick one or more random entries out of an array, string, or object. More on github.com
Removing a random element form an array
Hi there - love the library! Quick question - would the ability to remove a random element from the passed collection be a desirable feature for lodash? Something like this for instance? // I chose *bob* is in "bobbing for apples". Silly... More on github.com
Javascript- Lodash shuffle vs. Math.Random() - Stack Overflow
I'm in the process of coding a simple BlackJack game in Javascript. So far, I have an array like this: var deckArray = [ "card1", "card2",...,"card52" ] I have a "deal" function set up like this... More on stackoverflow.com
Sized array of random unique numbers
I was wondering what was the most concise way to get an array of a certain size, of unique random numbers. ... However this is not unique. I can filter this with uniq but I need to gurantee length of 4 of array. And I want to do it the lodash way. More on stackoverflow.com
Lodash
lodash.com › docs
Lodash Documentation
Gets n random elements at unique keys from collection up to the size of collection. ... Creates an array of shuffled values, using a version of the Fisher-Yates shuffle.
GitHub
github.com › lodash › lodash › issues › 3572
Feature Requests: Pick one or more random entries out of an array, string, or object · Issue #3572 · lodash/lodash
November 19, 2017 - Pick one or more random entries out of an array, string, or object. Use case: string // in Bulls and Cows game function genSecret() { return _.shuffle("0123456789".split("")).slice(0, 4).join("") } // with new method function genSecret()...
Author taichunmin
JSFiddle
jsfiddle.net › dawidrylko › 8haxhvps
Get random item from an array [Lodash] - JSFiddle - Code Playground
JSFiddle - Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle.
GeeksforGeeks
geeksforgeeks.org › lodash-_-random-method
Lodash _.random() Method | GeeksforGeeks
October 31, 2023 - If a properties object is given, ... property_object);Parameters: proto_obj: This is the object to inherit from.p ... Lodash is a JavaScript utility library built on top of Underscore.js. It simplifies working with arrays, strings, objects, numbers, and more....
Peaceful Revolution
wiserfirst.com › blog › typescript-random-array-element
Get Random Array Element in Typescript | Peaceful Revolution
July 24, 2021 - For an array, the lowest valid index is 0 and the highest valid index is arr.length - 1. I could generate an random integer between 0 and arr.length - 1. Then if I use that number as index for the array, I could get back a random element. That should look like this: ... It does the job and it’s fairly straightforward. But there is actually a small issue, which I’ll address in later. After implementing it, I thought that there is a _.sample function from the popular library lodash, it would be interesting to see how it was implemented.
TutorialsPoint
tutorialspoint.com › lodash › lodash_random.htm
Lodash - random method
Produces a random number between the inclusive lower and upper bounds. If only one argument is provided a number between 0 and the given number is returned. If floating is true, or either lower or upper are floats, a floating-point number is ...
GitHub
github.com › lodash › lodash › issues › 801
Removing a random element form an array · Issue #801 · lodash/lodash
November 28, 2014 - Quick question - would the ability to remove a random element from the passed collection be a desirable feature for lodash? ... // I chose *bob* is in "bobbing for apples". Silly, yes. function bob(collection, n) { var index = collection ? baseRandom(0, collection.length -1) : -1, collection = collection || []; return collection.splice(index)[0]; } array = [1,2,3,4,5] _.bob(array) => 2 array => [1, 3, 4, 5]
Author nicohvi
GeeksforGeeks
geeksforgeeks.org › lodash-_-shuffle-method
Lodash _.shuffle() Method | GeeksforGeeks
September 3, 2024 - Lodash _.shuffle() method creates an array of shuffled values from the given collection using a version of the Fisher-Yates shuffle algorithm.
Top answer 1 of 3
3
Much easiear would be...
const uniqRandomNumbers = _.sampleSize(_.range(30, 95), 4);
console.log(uniqRandomNumbers);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
2 of 3
2
I know this isn't "the lodash way", but it guarantees uniqueness, and allows you to use the same arguments as you were using before. It also scales better than methods that require a binary or linear search through an array, as set.has() is O(1) on average, rather than O(log(n)) or O(n).
function uniqRandom (times, ...args) {
const set = new Set()
while (times > 0) {
const rand = _.random(...args)
if (!set.has(rand)) {
set.add(rand)
times--
}
}
return Array.from(set)
}
console.log(uniqRandom(4, 30, 33));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
JavaScript in Plain English
javascript.plainenglish.io › how-to-get-a-random-item-from-a-javascript-array-801be95dce13
How to Get a Random Item From a JavaScript Array? | by John Au-Yeung | JavaScript in Plain English
November 10, 2021 - Then we can use that to get an element from the array. ... Since Math.random returns a number between 0 and 1, we’ve to multiply the returned number by items.length to get an index. Also, we’ve to use Math.floor to round the number down to the nearest integer to get an index. Lodash has various handy methods we can use to get a random item from an array.