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
128
votes
8 answers

Shuffle two list at once with same order

I'm using the nltk library's movie_reviews corpus which contains a large number of documents. My task is get predictive performance of these reviews with pre-processing of the data and without pre-processing. But there is problem, in lists…
Jaroslav Klimčík
  • 4,548
  • 12
  • 39
  • 58
113
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
  • 2,138
  • 4
  • 21
  • 31
100
votes
4 answers

shuffle vs permute numpy

What is the difference between numpy.random.shuffle(x) and numpy.random.permutation(x)? I have read the doc pages but I could not understand if there was any difference between the two when I just want to randomly shuffle the elements of an…
DotPi
  • 3,977
  • 6
  • 33
  • 53
98
votes
2 answers

How to randomize a vector

I would like to randomly reorganize the order of the numbers in a vector, in a simple one-line command? My particular vector V has 150 entries for each value from 1 to 10: V <- rep(1:10, each=150)
user1723765
  • 6,179
  • 18
  • 57
  • 85
79
votes
5 answers

Numpy shuffle multidimensional array by row only, keep column order unchanged

How can I shuffle a multidimensional array by row only in Python (so do not shuffle the columns). I am looking for the most efficient solution, because my matrix is very huge. Is it also possible to do this highly efficient on the original array (to…
robert
  • 1,921
  • 2
  • 17
  • 27
78
votes
4 answers

What, if anything, is wrong with this shuffling algorithm and how can I know?

Just as background, I'm aware of the Fisher-Yates perfect shuffle. It is a great shuffle with its O(n) complexity and its guaranteed uniformity and I'd be a fool not to use it ... in an environment that permits in-place updates of arrays (so in…
JUST MY correct OPINION
  • 35,674
  • 17
  • 77
  • 99
73
votes
10 answers

What distribution do you get from this broken random shuffle?

The famous Fisher-Yates shuffle algorithm can be used to randomly permute an array A of length N: For k = 1 to N Pick a random integer j from k to N Swap A[k] and A[j] A common mistake that I've been told over and over again not to make is…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
52
votes
13 answers

Most efficient way to randomly "sort" (Shuffle) a list of integers in C#

I need to randomly 'sort' a list of integers (0-1999) in the most efficient way possible. Any ideas? Currently, I am doing something like this: bool[] bIndexSet = new bool[iItemCount]; for (int iCurIndex = 0; iCurIndex < iItemCount;…
Carl
  • 2,415
  • 3
  • 17
  • 7
45
votes
4 answers

Random order of rows Matlab

Say we have a matrix of size 100x3 How would you shuffle the rows in MATLAB?
edgarmtze
  • 24,683
  • 80
  • 235
  • 386
41
votes
2 answers

Java's Collections.shuffle is doing what?

I recently found myself needing to be sure my list wasn't in order. Hibernate was nice enough to return it in perfect order. Silly hibernate, not reading my mind. I looked at my Java API and it tells me its shuffle method does this: Randomly…
Stephano
  • 5,716
  • 7
  • 41
  • 57
39
votes
8 answers

Better way to shuffle two related lists

Is there better ways to randomly shuffle two related lists without breaking their correspondence in the other list? I've found related questions in numpy.array and c# but not exactly the same one. As a first try, a simple zip trick will do: import…
clwen
  • 20,004
  • 31
  • 77
  • 94
36
votes
7 answers

Shuffling non-zero elements of each row in an array - Python / NumPy

I have a an array that is relatively sparse, and I would like to go through each row and shuffle only the non-zero elements. Example Input: [2,3,1,0] [0,0,2,1] Example Output: [2,1,3,0] [0,0,1,2] Note how the zeros have not changed position. To…
Bar
  • 2,736
  • 3
  • 33
  • 41
34
votes
4 answers

Maximal Length of List to Shuffle with Python random.shuffle?

I have a list which I shuffle with the Python built in shuffle function (random.shuffle) However, the Python reference states: Note that for even rather small len(x), the total number of permutations of x is larger than the period of most random…
Henrik
  • 14,202
  • 10
  • 68
  • 91
32
votes
1 answer

What's a good one-pass pseudo-random shuffle?

The Fisher-Yates shuffle gives a nice algorithm to shuffle an array A of length n in a single pass: For k = 1 to n Pick a random integer j from k to n Swap A[k] and A[j] After a single pass through this algorithm, the entries of A occur…
PengOne
  • 48,188
  • 17
  • 130
  • 149
31
votes
1 answer

Scala ListBuffer (or equivalent) shuffle

Is there a simple shuffle function for Scala lists? If not, whats the simplest way to implement? I have a lot of these things to do all over the code, so the simpler the call, the best it is An example in Ruby a = [ 1, 2, 3 ] #=> [1, 2,…
rdlu
  • 630
  • 6
  • 10