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
19
votes
3 answers

Shuffling a poker deck in JavaScript with window.crypto.getRandomValues

A poker deck has 52 cards and thus 52! or roughly 2^226 possible permutations. Now I want to shuffle such a deck of cards perfectly, with truly random results and a uniform distribution, so that you can reach every single one of those possible…
caw
  • 30,999
  • 61
  • 181
  • 291
19
votes
3 answers

Shuffle a pandas dataframe by groups

My dataframe looks like this sampleID col1 col2 1 1 63 1 2 23 1 3 73 2 1 20 2 2 94 2 3 99 3 1 73 3 2 56 3 3 34 I need to shuffle the…
Test Test
  • 485
  • 1
  • 6
  • 11
19
votes
17 answers

How to shuffle characters in a string without using Collections.shuffle(...)?

How do I shuffle the characters in a string (e.g. hello could be ehlol or lleoh or ...). I don't want to use the Collections.shuffle(...) method, is there anything simpler?
user339108
  • 12,613
  • 33
  • 81
  • 112
19
votes
7 answers

Controlling distance of shuffling

I have tried to ask this question before, but have never been able to word it correctly. I hope I have it right this time: I have a list of unique elements. I want to shuffle this list to produce a new list. However, I would like to constrain the…
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
19
votes
5 answers

Can Python's set absence of ordering be considered random order?

I'd like to know if the absence of element ordering of the Python's built-in set structure is "random enough". For instance, taking the iterator of a set, can it be considered a shuffled view of its elements? (If it matters, I'm running Python 2.6.5…
Chuim
  • 1,983
  • 3
  • 17
  • 20
19
votes
8 answers

shuffle a large list of items without loading in memory

I have a file with ~2 billion lines of text (~200gigs). I want to produce a new file containing the same text lines, but shuffled randomly by line. I can't hold all the data in memory. Is there a good way to do this in python/command line that takes…
daemonk
  • 427
  • 6
  • 9
18
votes
6 answers

Shuffle in Python

Is there a straightforward way to RETURN a shuffled array in Python rather than shuffling it in place? e.g., instead of x = [array] random.shuffle(x) I'm looking for something like y = shuffle(x) which maintains x. Note, I am not looking for a…
Jeff
  • 12,147
  • 10
  • 51
  • 87
18
votes
7 answers

How random is PHP's shuffle function?

Does anyone know what's the randomness of PHP's shuffle() function? Does it depend on the operating system? Does it use PHP's own seeder? Is it possible to use mt_rand() as generator?
Sinan
  • 5,819
  • 11
  • 39
  • 66
17
votes
1 answer

python shuffle algorithm performance

I was wondering about the time complexity of the shuffle function in the random Python library/module. Is it O(n) or is it less than that? Is there a website that shows the time complexities of functions that belong to Python libraries?
Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152
17
votes
2 answers

Why does the Collections.shuffle() algorithm work better than my implementation

Collections.shuffle() goes through each index of a Collection backwards and then swaps it with a random index including or before it. I was wondering why, so I tried doing the same thing but swapping with any random index in the Collection. Here is…
czhao
  • 305
  • 2
  • 7
17
votes
4 answers

Shuffle List

Possible Duplicate: Randomize a List in C# I have a list which contains many thousands of FilePath's to locations of audio files, and was wondering which would be the most efficient way to "shuffle" a List? Any help is greatly appreciated…
anon271334
17
votes
1 answer

What’s the best way to shuffle an array in Perl?

So I have a file. Let’s say it looks like this (it's actually longer): 1234 2134 3124 4123 What is the best way to shuffle the lines in that file?
Bill
  • 1,237
  • 4
  • 21
  • 44
16
votes
7 answers

C#: Good/best implementation of Swap method

I read this post about card shuffling and in many shuffling and sorting algorithms you need to swap two items in a list or array. But what does a good and efficient Swap method look like? Let's say for a T[] and for a List. How would you best…
Svish
  • 152,914
  • 173
  • 462
  • 620
16
votes
1 answer

Spill to disk and shuffle write spark

I'm getting confused about spill to disk and shuffle write. Using the default Sort shuffle manager, we use an appendOnlyMap for aggregating and combine partition records, right? Then when execution memory fill up, we start sorting map, spilling it…
Giorgio
  • 1,073
  • 3
  • 15
  • 33
16
votes
5 answers

How does random shuffling in quick sort help in increasing the efficiency of the code?

I was going through lecture videos by Robert Sedgwick on algorithms, and he explains that random shuffling ensures we don't get to encounter the worst case quadratic time scenario in quick sort. But I am unable to understand how.
Paritosh Pandey
  • 167
  • 1
  • 4