Questions tagged [shuffle]

Shuffling is the act of randomizing the order of the elements in a collection.

Shuffling is the process by which the order of the elements in a collection is randomized. The term shuffling is often used for playing cards.

One effective algorithm for doing so is Fischer-Yates Shuffle:

For a zero-based collection of n elements:

  1. Let i = n

  2. Let j = random integer between 0 and i (inclusive).

  3. Swap the values of the elements at j and i

  4. i = i - 1

  5. if (i > 0), go to Step-2

The algorithm has a time complexity of O(n).

2015 questions
1493
votes
58 answers

How to randomize (shuffle) a JavaScript array?

I have an array like this: var arr1 = ["a", "b", "c", "d"]; How can I randomize / shuffle it?
Click Upvote
  • 235,452
  • 251
  • 553
  • 736
834
votes
24 answers

Shuffling a list of objects

I have a list of objects and I want to shuffle them. I thought I could use the random.shuffle method, but this seems to fail when the list is of objects. Is there a method for shuffling objects or another way around this? import random class A: …
utdiscant
  • 10,402
  • 7
  • 29
  • 38
580
votes
10 answers

Shuffle DataFrame rows

I have the following DataFrame: Col1 Col2 Col3 Type 0 1 2 3 1 1 4 5 6 1 ... 20 7 8 9 2 21 10 11 12 2 ... 45 13 14 15 3 46 16 17 18 3 ... The DataFrame…
JNevens
  • 8,412
  • 7
  • 35
  • 67
317
votes
25 answers

How do I shuffle an array in Swift?

How do I randomize or shuffle the elements within an array in Swift? For example, if my array consists of 52 playing cards, I want to shuffle the array in order to shuffle the deck.
mpatzer
  • 4,571
  • 3
  • 20
  • 30
299
votes
19 answers

How can I shuffle the lines of a text file on the Unix command line or in a shell script?

I want to shuffle the lines of a text file randomly and create a new file. The file may have several thousands of lines. How can I do that with cat, awk, cut, etc?
Ruggiero Spearman
  • 6,165
  • 5
  • 24
  • 36
294
votes
11 answers

Shuffle an array with python, randomize array item order with python

What's the easiest way to shuffle an array with python?
davethegr8
  • 10,495
  • 5
  • 32
  • 60
279
votes
16 answers

Better way to shuffle two numpy arrays in unison

I have two numpy arrays of different shapes, but with the same length (leading dimension). I want to shuffle each of them, such that corresponding elements continue to correspond -- i.e. shuffle them in unison with respect to their leading…
Josh Bleecher Snyder
  • 7,547
  • 2
  • 33
  • 36
255
votes
31 answers

Random shuffling of an array

I need to randomly shuffle the following Array: int[] solutionArray = {1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1}; Is there any function to do that?
Hubert
  • 15,094
  • 18
  • 42
  • 51
191
votes
12 answers

What's the Best Way to Shuffle an NSMutableArray?

If you have an NSMutableArray, how do you shuffle the elements randomly? (I have my own answer for this, which is posted below, but I'm new to Cocoa and I'm interested to know if there is a better way.) Update: As noted by @Mukesh, as of iOS 10+…
Kristopher Johnson
  • 76,675
  • 54
  • 235
  • 299
169
votes
13 answers

Is using Random and OrderBy a good shuffle algorithm?

I have read an article about various shuffle algorithms over at Coding Horror. I have seen that somewhere people have done this to shuffle a list: var r = new Random(); var shuffled = ordered.OrderBy(x => r.Next()); Is this a good shuffle…
Svish
  • 138,188
  • 158
  • 423
  • 589
130
votes
5 answers

How to randomly sort (scramble) an array in Ruby?

I'd like to have my array items scrambled. Something like this: [1,2,3,4].scramble => [2,1,3,4] [1,2,3,4].scramble => [3,1,2,4] [1,2,3,4].scramble => [4,2,3,1] and so on, randomly
Daniel Cukier
  • 10,639
  • 12
  • 61
  • 115
127
votes
12 answers

Is it correct to use JavaScript Array.sort() method for shuffling?

I was helping somebody out with his JavaScript code and my eyes were caught by a section that looked like that: function randOrd(){ return (Math.round(Math.random())-0.5); } coords.sort(randOrd); alert(coords); My first though was: hey, this…
Rene Saarsoo
  • 12,403
  • 8
  • 51
  • 75
126
votes
10 answers

What is the purpose of shuffling and sorting phase in the reducer in Map Reduce Programming?

In Map Reduce programming the reduce phase has shuffling, sorting and reduce as its sub-parts. Sorting is a costly affair. What is the purpose of shuffling and sorting phase in the reducer in Map Reduce Programming?
Nithin K Anil
  • 7,707
  • 12
  • 38
  • 59
107
votes
6 answers

How to shuffle a std::vector?

I am looking for a generic, reusable way to shuffle a std::vector in C++. This is how I currently do it, but I think it's not very efficient because it needs an intermediate array and it needs to know the item type (DeckCard in this…
laurent
  • 79,308
  • 64
  • 256
  • 389
106
votes
11 answers

How can I randomize the lines in a file using standard tools on Red Hat Linux?

How can I randomize the lines in a file using standard tools on Red Hat Linux? I don't have the shuf command, so I am looking for something like a perl or awk one-liner that accomplishes the same task.
Stuart Woodward
  • 1,919
  • 4
  • 18
  • 29
1
2 3
99 100