Get random array from array javascript
Just two lines : Show
DEMO:
answered Jul 25, 2016 at 14:57
Abdennour TOUMIAbdennour TOUMI 79.3k36 gold badges233 silver badges238 bronze badges 7 Try this non-destructive (and fast) function:
answered Oct 9, 2013 at 10:52
13 There is a one-liner unique solution here
answered Sep 27, 2019 at 2:26
Olalekan SogunleOlalekan Sogunle 2,2391 gold badge19 silver badges26 bronze badges 2 lodash Gets one or n random elements at unique keys from collection up to the size of collection.
genericUser 2,0121 gold badge15 silver badges29 bronze badges answered Apr 9, 2020 at 3:04
nodejhnodejh 7,7581 gold badge18 silver badges23 bronze badges 2 Getting 5 random items without changing the original array:
(Don't use this for big lists) answered Mar 25, 2018 at 19:12
pomberpomber 21.7k10 gold badges75 silver badges91 bronze badges 2 create a funcion which does that:
you should also check if the sourceArray has enough elements to be returned. and if you want unique elements returned, you should remove selected element from the sourceArray. answered Oct 9, 2013 at 10:37
2 Porting
Implementation ported from Lib/random.py. Notes:
Performance against the accepted answer:
user 20.7k9 gold badges109 silver badges98 bronze badges answered Aug 7, 2017 at 22:55
Derek 朕會功夫Derek 朕會功夫 89.6k41 gold badges177 silver badges242 bronze badges 1 If you want to randomly get items from the array in a loop without repetitions you can remove the selected item from the array with
answered Oct 9, 2013 at 10:41
Rory McCrossanRory McCrossan 324k37 gold badges295 silver badges324 bronze badges 2 ES6 syntax
answered Mar 27, 2019 at 9:58
Yair LevyYair Levy 1,04713 silver badges11 bronze badges 1 I can't believe that no one didn't mention this method, pretty clean and straightforward.
answered Feb 13, 2020 at 21:13
DedaDevDedaDev 3,3191 gold badge17 silver badges26 bronze badges 3
answered Oct 9, 2013 at 10:47
0 Here's a nicely typed version. It doesn't fail. Returns a shuffled array if sample size is larger than original array's length.
answered Jul 20, 2019 at 12:52
In this answer, I want to share with you the test that I have to know the best method that gives equal chances for all elements to have random subarray. Method 01
using this method, some elements have higher chances comparing with others.
Method 2Using this method, the elements have the same probability:
The correct answer is posted in in the following link: https://stackoverflow.com/a/46545530/3811640 answered Dec 3, 2019 at 13:23
haouarinhaouarin 1,3319 silver badges8 bronze badges 2020
answered Oct 4, 2020 at 10:24
3 EDIT: This solution is slower than others presented here (which splice the source array) if you want to get only a few elements. The speed of this solution depends only on the number of elements in the original array, while the speed of the splicing solution depends on the number of elements required in the output array. If you want non-repeating random elements, you can shuffle your array then get only as many as you want:
DEMO: http://jsbin.com/UHUHuqi/1/edit Shuffle function taken from here: https://stackoverflow.com/a/6274398/1669279
answered Oct 9, 2013 at 10:40
TibosTibos 27.1k4 gold badges47 silver badges62 bronze badges 6 I needed a function to solve this kind of issue so I'm sharing it here.
Note: if Demo answered Feb 26, 2019 at 16:12
Storage LenovoStorage Lenovo 851 gold badge1 silver badge6 bronze badges
Here's an optimized version of the code ported from Python by @Derek, with the added destructive (in-place) option that makes it the fastest algorithm possible if you can go with it. Otherwise it either makes a full copy or, for a small number of items requested from a large array, switches to a selection-based algorithm.
In comparison to Derek's implementation, the first algorithm is much faster in Firefox while being a bit slower in Chrome, although now it has the destructive option - the most performant one. The second algorithm is simply 5-15% faster. I try not to give any concrete numbers since they vary depending on k and n and probably won't mean anything in the future with the new browser versions. The heuristic that makes the choice between algorithms originates from Python code. I've left it as is, although it sometimes selects the slower one. It should be optimized for JS, but it's a complex task since the performance of corner cases is browser- and their version-dependent. For example, when you try to select 20 out of 1000 or 1050, it will switch to the first or the second algorithm accordingly. In this case the first one runs 2x faster than the second one in Chrome 80 but 3x slower in Firefox 74. answered Apr 7, 2020 at 10:46
useruser 20.7k9 gold badges109 silver badges98 bronze badges 3 Sampling with possible duplicates:
Sampling without duplicates:
Since without
duplicates requires sorting the whole index array first, it is considerably slow than with possible duplicates for big Obviously, the max size of without duplicates is <= Check this fiddle: https://jsfiddle.net/doleron/5zw2vequ/30/ answered Jan 31 at 6:25
DulorenDuloren 2,1131 gold badge22 silver badges30 bronze badges 1 It extracts random elements from srcArray one by one while it get's enough or there is no more elements in srcArray left for extracting. Fast and reliable.
answered Aug 30, 2018 at 17:46
1 Here's a function I use that allows you to easily sample an array with or without replacement:
Using it is simple: Without Replacement (default behavior)
With Replacement
answered Jul 27, 2019 at 8:37
Jared WilberJared Wilber 5,13329 silver badges34 bronze badges
answered Sep 16, 2019 at 7:04
Manoj RanaManoj Rana 2,5081 gold badge23 silver badges26 bronze badges
answered Aug 13 at 10:03
1 Here is the most correct answer and it will give you Random + Unique elements.
answered Dec 26, 2016 at 11:10
2 items.sort(() => (Math.random() > 0.5 ? 1 : -1)).slice(0, count); answered Dec 27, 2019 at 15:38
1 2019 This is same as Laurynas Mališauskas answer, just that the elements are unique (no duplicates).
Now to answer original question "How to get multiple random elements by jQuery", here you go:
answered Feb 17, 2019 at 12:27
evilReikoevilReiko 18.4k24 gold badges82 silver badges99 bronze badges 2 Not the answer you're looking for? Browse other questions tagged javascript jquery html arrays or ask your own question.How do you get random elements from an array?To get multiple random elements from an array, use the sort() method on the array to shuffle the array elements in a random order, e.g. arr. sort(() => 0.5 - Math. random()) . Then call the slice() method on the shuffled array to get multiple random elements.
How do you shuffle an array?There are two ways to shuffle an array in Java.. Collections.shuffle() Method.. Random Class.. What does math random () do?The Math. random() function 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.
How do you generate a random number in Javascript?Javascript creates pseudo-random numbers with the function Math. random() . This function takes no parameters and creates a random decimal number between 0 and 1. The returned value may be 0, but it will never be 1.
|