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

Shuffle a numpy array

I have a 2-d numpy array that I would like to shuffle. Is the best way to reshape it to 1-d, shuffle and reshape again to 2-d or is it possible to shuffle without reshaping? just using the random.shuffle doesn't yield expected results and…
Artturi Björk
  • 3,643
  • 6
  • 27
  • 35
16
votes
6 answers

Combining two different 'ranges' to one in ruby

I want to combine two different ranges in rails into a single array. Is there any short method for the same? I am writing code to generate random alpha-numeric strings. Right now I have: ('a'..'z').to_a.shuffle.first(16).join I have also tried…
Radhika
  • 2,453
  • 2
  • 23
  • 28
16
votes
6 answers

Python shuffle such that position will never repeat

I'd like to do a random shuffle of a list but with one condition: an element can never be in the same original position after the shuffle. Is there a one line way to do such in python for a list? Example: list_ex = [1,2,3] each of the following…
Dnaiel
  • 7,622
  • 23
  • 67
  • 126
15
votes
3 answers

How to shuffle an array of objects in javascript?

The code below works for a normal array but not with an array with object does anybody knows how to do this? const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; function shuffle(array) { for (let i = array.length - 1; i > 0; i--) { let j =…
Arne
  • 171
  • 1
  • 1
  • 4
15
votes
4 answers

Randomly shuffle data and labels from different files in the same order

l have two numpy arrays the first one contains data and the second one contains labels. l want to shuffle the data with respect to their labels. In other way, how can l shuffle my labels and data in the same order. import numpy as…
vincent
  • 1,558
  • 4
  • 21
  • 34
15
votes
1 answer

Understanding shuffle managers in Spark

Let me help to clarify about shuffle in depth and how Spark uses shuffle managers. I report some very helpful…
Giorgio
  • 1,073
  • 3
  • 15
  • 33
15
votes
9 answers

Card Shuffling in C#

I am trying to write a code for a project that lists the contents of a deck of cards, asks how much times the person wants to shuffle the deck, and then shuffles them. It has to use a method to create two random integers using the System.Random…
Jeff
14
votes
3 answers

php random order from a foreach

I have some foreach, this could work well foreach ($umm as $data) { echo ''; echo $data->id; } Now I want shuffle the foreach. I tried: foreach (shuffle($umm) as $data) { echo '
fish man
  • 2,666
  • 21
  • 54
  • 94
14
votes
5 answers

Card Game: Randomly pick 1 number out of array of 52 without duplicates

I have a simple card game (using 52 cards - no jokers) that I want to randomly pick 1 card at a time until the winning card is chosen. I have the following array: $cards = array( 'diamond' => array( 'A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J',…
Tech4Wilco
  • 6,740
  • 5
  • 46
  • 81
14
votes
6 answers

How to make sure that std::random_shuffle always produces a different result?

Is there some function, similar to srand(), that I need to call to make sure that std::random_shuffle() always produces different results? i.e. if I call it several times with the same data, I want the order to be different every time. How can I…
laurent
  • 88,262
  • 77
  • 290
  • 428
14
votes
3 answers

Efficient random generator for very large range (in python)

I am trying to create a generator that returns numbers in a given range that pass a particular test given by a function foo. However I would like the numbers to be tested in a random order. The following code will achieve this: from random import…
tim-mccurrach
  • 6,395
  • 4
  • 23
  • 41
14
votes
2 answers

How can I shuffle a whole dataset with TensorFlow?

Now I use following function for shuffling from tensorflow.contrib import data def input_pipeline(filenames, batch_size): # Define a `tf.contrib.data.Dataset` for iterating over one epoch of the data. dataset =…
danche
  • 1,775
  • 15
  • 22
14
votes
6 answers

Select cells randomly from NumPy array - without replacement

I'm writing some modelling routines in NumPy that need to select cells randomly from a NumPy array and do some processing on them. All cells must be selected without replacement (as in, once a cell has been selected it can't be selected again, but…
robintw
  • 27,571
  • 51
  • 138
  • 205
14
votes
6 answers

Efficiently pick n random elements from PHP array (without shuffle)

I have the following code to pick $n elements from an array $array in PHP: shuffle($array); $result = array_splice($array, 0, $n); Given a large array but only a few elements (for example 5 out of 10000), this is relatively slow, so I would like to…
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
14
votes
2 answers

Knowing an item's location in an array

I want to switch J1's position with the card under it after the deck array is shuffled. Is there a way to reference J1 without knowing its position in the array? Thank you. import random deck = ['AC', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C',…
Carm
  • 203
  • 1
  • 7