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

An extension method on IEnumerable needed for shuffling

I need an extension method which will shuffle an IEnumerable. It can also take an int to specify the size of the returned IEnumerable. Better keeping Immutability of the IEnumerable. My current solution for IList- public static IList
Gulshan
  • 3,611
  • 5
  • 35
  • 46
29
votes
6 answers

Get random elements from array in Swift

I have an array like: var names: String = [ "Peter", "Steve", "Max", "Sandra", "Roman", "Julia" ] I would like to get 3 random elements from that array. I'm coming from C# but in swift I'm unsure where to start. I think I should shuffle the array…
Patric
  • 2,789
  • 9
  • 33
  • 60
28
votes
3 answers

Spark: Difference between Shuffle Write, Shuffle spill (memory), Shuffle spill (disk)?

I have the following spark job, trying to keep everything in memory: val myOutRDD = myInRDD.flatMap { fp => val tuple2List: ListBuffer[(String, myClass)] = ListBuffer() : tuple2List }.persist(StorageLevel.MEMORY_ONLY).reduceByKey { (p1,…
Edamame
  • 23,718
  • 73
  • 186
  • 320
27
votes
5 answers

Is it possible to random_shuffle an array of int elements?

I was reading up on this : http://www.cplusplus.com/reference/algorithm/random_shuffle/ and wondered if its possible to random_shuffle an array of int elements. This is my code #include #include using namespace std; int…
Computernerd
  • 7,378
  • 18
  • 66
  • 95
27
votes
7 answers

Algorithm for Shuffling a Linked List in n log n time

I'm trying to shuffle a linked list using a divide-and-conquer algorithm that randomly shuffles a linked list in linearithmic (n log n) time and logarithmic (log n) extra space. I'm aware that I can do a Knuth shuffle similar to that could be used…
5StringRyan
  • 3,604
  • 5
  • 46
  • 69
26
votes
7 answers

How to use random.shuffle() on a generator? python

How do I use random.shuffle() on a generator without initializing a list from the generator? Is that even possible? if not, how else should I use random.shuffle() on my list? >>> import random >>> random.seed(2) >>> x = [1,2,3,4,5,6,7,8,9] >>> def…
alvas
  • 115,346
  • 109
  • 446
  • 738
25
votes
5 answers

Generating shuffled range using a PRNG rather than shuffling

Is there any known algorithm that can generate a shuffled range [0..n) in linear time and constant space (when output produced iteratively), given an arbitrary seed value? Assume n may be large, e.g. in the many millions, so a requirement to…
Barry Kelly
  • 41,404
  • 5
  • 117
  • 189
23
votes
11 answers

Why does this simple shuffle algorithm produce biased results?

It seems that this simple shuffle algorithm will produce biased results: # suppose $arr is filled with 1 to 52 for ($i < 0; $i < 52; $i++) { $j = rand(0, 51); # swap the items $tmp = $arr[j]; $arr[j] = $arr[i]; $arr[i] = $tmp; } You…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
23
votes
4 answers

Why does Collections.shuffle() fail for my array?

Why does my code not work? package generatingInitialPopulation; import java.util.Arrays; import java.util.Collections; public class TestShuffle { public static void main(String[] args) { int[] arr = new int[10]; for (int i =…
Dmitry
  • 3,028
  • 6
  • 44
  • 66
23
votes
3 answers

JavaScript - Shuffling Objects inside an object (randomize)

I need to implement a randomization from JSON result. The format of the JSON is two objects: result: Question(object) [Object { id="4c6e9a41470b19_96235904", more...}, Object { id="4c784e6e928868_58699409", more...}, Object {…
easement
  • 6,119
  • 3
  • 29
  • 36
21
votes
1 answer

Shuffle a List in Scala

I have question to for shuffle list in scala using scala.util.Random. For example I have val a = cyan val b = magenta val c = yellow val d = key val color = Random.shuffle.List(a,b,c,d).toString //but it doesn't work ;( so I want the val color to…
Deden Bangkit
  • 598
  • 1
  • 5
  • 19
21
votes
3 answers

Choosing k out of n

I want to choose k elements uniformly at random out of a possible n without choosing the same number twice. There are two trivial approaches to this. Make a list of all n possibilities. Shuffle them (you don't need to shuffle all n numbers just k…
Benjy Kessler
  • 7,356
  • 6
  • 41
  • 69
20
votes
3 answers

Shuffle list, ensuring that no item remains in same position

I want to shuffle a list of unique items, but not do an entirely random shuffle. I need to be sure that no element in the shuffled list is at the same position as in the original list. Thus, if the original list is (A, B, C, D, E), this result would…
jdeisenberg
  • 201
  • 2
  • 4
20
votes
4 answers

How to implement a repeating shuffle that's random - but not too random

I've got a list of n items. I want an algorithm to let me pick a potentially infinite sequence of items from that collection at random, but with a couple of constraints: once an item has been picked, it shouldn't turn up again in the next few items…
James Hart
  • 1,251
  • 1
  • 9
  • 11
19
votes
8 answers

Python - shuffle only some elements of a list

I'm trying to shuffle only elements of a list on 3rd till last position so the 1st two will always stay in place e.g. list = ['a?','b','c','d','e'] into list = ['a?','b','d','e','c'] and for some reason this doesn't work: list =…
user1227065