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
14
votes
10 answers

What is the best List implementation for Large lists in java

I have to create a large list of n elements (could be up to 100,000). each element in the list is an integer equivalent to the index of the list. After this I have to call Collections.shuffle on this list. My question is, which list implementation…
rabbit
  • 141
  • 1
  • 1
  • 4
14
votes
2 answers

Shuffle a list within a specific range python

I'm very new to Python, so bear with me. I would like to run a program that will shuffle a string of integers in a specific range, but without having to input each integer within that range. I.e., I want to randomize the list of integers from 1 to…
common_currency
  • 141
  • 1
  • 1
  • 5
13
votes
2 answers

Fisher Yates variation

The classic Fisher Yates looks something like this: void shuffle1(std::vector& vec) { int n = vec.size(); for (int i = n - 1; i > 0; --i) { std::swap(vec[i], vec[rand() % (i + 1)]); } } Yesterday, I implemented the…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
13
votes
3 answers

Simple method to shuffle the elements of an array in BASH shell?

I can do this in PHP but am trying to work within the BASH shell. I need to take an array and then randomly shuffle the contents and dump that to somefile.txt. So given array Heresmyarray, of elements a;b;c;d;e;f; it would produce an output file,…
Dave
  • 155
  • 1
  • 2
  • 7
13
votes
3 answers

Scikit-learn, GroupKFold with shuffling groups?

I was using StratifiedKFold from scikit-learn, but now I need to watch also for "groups". There is nice function GroupKFold, but my data are very time dependent. So similary as in help, ie number of week is the grouping index. But each week should…
gugatr0n1c
  • 377
  • 6
  • 21
13
votes
11 answers

Algorithm to print out a shuffled list, in-place and with O(1) memory

After reading this question I started to wonder: is it possible to have a shuffling algorithm which does not modify or copy the original list? To make it clear: Imagine you are given a list of objects. The list size can be arbitrary, but assume it's…
Vilx-
  • 104,512
  • 87
  • 279
  • 422
13
votes
5 answers

How to really shuffle a deck of cards

When I need to shuffle a deck of poker cards in Java/Android, I use Collections.shuffle(List list), of course. I've ever been doing this and the results seemed acceptable. But they aren't. As outlined in this paper, there are 52! possible unique…
caw
  • 30,999
  • 61
  • 181
  • 291
12
votes
6 answers

Is Collections.shuffle() really random enough? Practical examples seem to deny this statement

I have 1000 unique objects in a java.util.List, each referring to an image, each image in the 1000-list is unique and now I'd like to shuffle them, so that I can use the first 20 objects and present them to the website-user. The user can then click…
basZero
  • 4,129
  • 9
  • 51
  • 89
12
votes
3 answers

Shuffling a list in Mathematica

What's the best/easiest way to shuffle a long list in Mathematica?
Szabolcs
  • 24,728
  • 9
  • 85
  • 174
12
votes
3 answers

Shuffling a deck of cards

I'm making a Deck class for a C++ program. It needs to have two methods: one to pop a card off the top of the deck, another to shuffle the deck. I'm concerned with the latter. Cards are represented as integers 1 to 52 inclusive. What is the fastest…
oadams
  • 3,019
  • 6
  • 30
  • 53
12
votes
5 answers

How do I shuffle a Javascript Array ensuring each Index is in a new position in the new Array?

I have an Array of Objects, like so. var usersGoing = [ { user: 0 }, { user: 1 }, { user: 2 }, { user: 3 }, { user: 4 } ]; I need to shuffle this Array so that no Object remains in the same index as when it was instantiated,…
12
votes
2 answers

Behavior of shuffle on Set vs List using scala.util.Random

scala> Random.shuffle((1 to 10).toSet) res10: scala.collection.immutable.Set[Int] = Set(5, 10, 1, 6, 9, 2, 7, 3, 8, 4) scala> Random.shuffle((1 to 10).toSet) res11: scala.collection.immutable.Set[Int] = Set(5, 10, 1, 6, 9, 2, 7, 3, 8, 4) …
k r
  • 409
  • 2
  • 6
  • 13
11
votes
3 answers

Scramble Python List

Before I ask my question, let me get this straight... This is not a duplicate of Does anyone know a way to scramble the elements in a list? and Shuffle an array with python, randomize array item order with python. I'll explain why... I want to know…
CoffeeRain
  • 4,460
  • 4
  • 31
  • 50
11
votes
5 answers

Test Probabilistic Functions

I need a function which returns an array in random order. I want to ensure that it is randomish but I have no idea how one would go about writing the tests to ensure that the array really is random. I can run the code a bunch of times and see if I…
stimms
  • 42,945
  • 30
  • 96
  • 149
11
votes
3 answers

Can Fisher-Yates shuffle produce all playing card permutations?

I'm using the standard Fisher-Yates algorithm to randomly shuffle a deck of cards in an array. However, I'm unsure if this will actually produce a true distribution of all possible permutations of a real-world shuffled deck of cards. V8's…
James Simpson
  • 13,488
  • 26
  • 83
  • 108