Questions tagged [shuffle]

Shuffling is the act of randomizing the order of the elements in a collection. Sometimes the term shuffling is also used for reordering elements in a controllable way, e.g. the bytes within a vector, especially by respective instructions for SIMD architectures.

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).

2286 questions
1993
votes
71 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?
Ali
  • 261,656
  • 265
  • 575
  • 769
916
votes
26 answers

Shuffling a list of objects

How do I shuffle a list of objects? I tried random.shuffle: import random b = [object(), object()] print(random.shuffle(b)) But it outputs: None
utdiscant
  • 11,128
  • 8
  • 31
  • 40
821
votes
14 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
  • 11,202
  • 9
  • 46
  • 72
355
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
  • 11,323
  • 5
  • 36
  • 61
338
votes
18 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
  • 8,262
  • 3
  • 35
  • 37
337
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,735
  • 5
  • 26
  • 37
329
votes
25 answers

How do I shuffle an array in Swift?

.shuffle() and .shuffled() are part of Swift Original historic question: 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…
M-P
  • 4,909
  • 3
  • 25
  • 31
282
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
  • 16,012
  • 18
  • 45
  • 51
193
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
  • 81,409
  • 55
  • 245
  • 302
177
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
  • 152,914
  • 173
  • 462
  • 620
152
votes
5 answers

Why does random.shuffle return None?

Why is random.shuffle returning None in Python? >>> x = ['foo','bar','black','sheep'] >>> from random import shuffle >>> print shuffle(x) None How do I get the shuffled value instead of None?
alvas
  • 115,346
  • 109
  • 446
  • 738
142
votes
8 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
  • 9,661
  • 14
  • 44
  • 67
138
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
  • 11,502
  • 15
  • 68
  • 123
134
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
  • 88,262
  • 77
  • 290
  • 428
129
votes
13 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
  • 13,580
  • 8
  • 57
  • 85
1
2 3
99 100