Questions tagged [shuffle]

Shuffling is the act of randomizing the order of the elements in a collection.

Filter by
Sorted by
Tagged with
29
votes
9answers
11k views

Generate a list of 10,000 numbers in random order

I am in the middle of an interview process with a company I am very excited about. The position is for a Junior Developer for C# applications. I have a year of experience at my current position as ...
19
votes
3answers
2k views

Shuffling algorithm for a “guess that tune” game

I'm making a "guess that tune" game in Visual Basic 6 that is supposed to play each song in a random order: ...
15
votes
5answers
5k views

Shuffling a deck

My objective is to swap every element of a string array with a random element. ...
14
votes
3answers
644 views

Allowing the user to alphabetize or randomize the contents of a text file

As a newbie programmer, I decided to try my hand at making a program whose function actually might be useful to someone, somewhere. This utility takes a text file, reads it out to the user, displays ...
13
votes
2answers
7k views

Fisher-Yates modern shuffle algorithm

I have implemented the shuffling algorithm of Fisher-Yates in C++, but I've stumbled across the modulo bias. Is this random number generation by rand() correct for ...
12
votes
3answers
7k views

Python shuffle the middle letters in all the words of a sentence

The purpose of the program is to shuffle only the middle letters of each word in a sentence, leaving it still readable like this: Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn't ...
12
votes
5answers
2k views

Deck of cards with shuffle and sort functionality

Going through Head First C#'s Chapter 8: Enums and Collections, I learned about List<T>, as well as IComparable<T> ...
12
votes
1answer
941 views

Corrupting Java arithmetic through reflection

Inspired by a PPCG answer, I wrote this code, which shuffles the autoboxing caches of Number subclasses. This leads to, among other things, outputting ...
11
votes
4answers
1k views

random_iterator in C++

This is a small utility that permutes a C++ standard container without modifying the container being iterated: random_iterator.hpp ...
11
votes
3answers
8k views

Card shuffling with Fisher-Yates method

The code below takes a specified number of card decks, and shuffles them according to the Fisher-Yates method. Does this implementation have any bias? ...
11
votes
1answer
320 views

Secret Santa with Groups on Swift

I am working on an special version of Secret Santa with internal sub-groups. Typical Secret Santa (SS) requirements: Cannot be assigned to themselves Should be random Every participant should be ...
10
votes
3answers
865 views

Simple word shuffling game

I have made a game (I like to code when in my spare time) where words are taken at random from a list and displayed on a label. If they type it correctly, their ...
9
votes
5answers
626 views

Shuffling a list of track indices

This question has a follow-up: Follow up to Shuffling a list of track indices I have a bunch of paths in an ArrayList<String> and when a user has activated ...
8
votes
7answers
12k views

C# Card Shuffle

So, I took a stab at the ole C# Card Shuffle. I wanted to create my own genuine solution rather than copy someone else's. Any advice? ...
7
votes
4answers
8k views

Basic playing card generation and shuffle

This is my first attempt at Java so I appreciate any criticism or pointers. The program should generate a full set of given decks and then shuffle them. ...
7
votes
4answers
9k views

Shuffling function using rand()

I would like to hear your suggestions about my shuffling function in C++. I've decided to take advantage of using rand() to build a shuffling function. ...
7
votes
2answers
285 views

Reading lines from a file in random order

I originally wrote this as an answer to a question on Stack Overflow, but it turned out so nicely that I decided to post it here to see if I can make it even better. ...
6
votes
3answers
764 views

Follow up to Shuffling a list of track indices

This is the improved code to my last question: ...
6
votes
4answers
4k views

Given N, generate sequence of random numbers from 0 to N - 1 with no repetitions

Problem statement: Given an integer n, write a function to return an array of size n, containing each of the numbers from 0 to n-1 in a random order. The numbers may not repeat and each number must ...
6
votes
3answers
282 views

Simple word shuffling game - follow-up

From my previous question, I got great feedback which helped improve my code which I submitted. I went back and built upon that and came back to see if my code is an improvement. ...
6
votes
3answers
1k views

Shuffle a sorted array so that left portions contains even indices and right portion contains odd indices numbers

I have been solving the following problem: Input array: \$[7,4,2,5,1,9,6]\$ Output array: \$[1,4,6,9,7,5,2]\$ I tried to do it in the following way: Sort the array. So, Input array becomes \$[1,2,...
6
votes
3answers
833 views

Is the way I'm handling deck (card) generation the wisest way to do it?

My Deck Class: (In case it isn't clear, a Card is just an object with two enums, one for suit and one for value). I'm ...
6
votes
4answers
1k views

Selecting random elements from a `IList` with no repetitions

Description This is a utility algorithm that selects a number of elements from a source list without repeating elements. Background My use case is creating a random path for a NPC that has to ...
6
votes
3answers
2k views

Shuffling an array keeping some elements fixed

The task is simple to understand: how to shuffle an array keeping some elements fixed. Let us take an example: starting from the array [1, 2, 'A', 3, 'B']. Having ...
6
votes
1answer
282 views

Fisher-Yates shuffle in Scheme

There are many ways to shuffle lists; for example, Racket's built-in shuffle assigns each list element with a random number as a sort key, then sorts the list, ...
6
votes
1answer
155 views

Forth to the past - qsort

To make a long story short, I must revive my Forth. It is rusty; last time I did anything serious in Forth was 30 years ago (man, I am old). As an exercise, I translated an STL-like implementation of <...
6
votes
1answer
532 views

Shuffling an arbitrary list or sequence

After reading this question I wanted to write a short example of how a list or sequence could be shuffled in .net. The result turned out to not be very short: Program.cs ...
5
votes
4answers
4k views

Cards shuffling and dealing program

The program interacts between cards and four players among whom cards are to be distributed. The Program do the following function Creates a deck of cards. Shuffle the deck. Shows the deck. Deal ...
5
votes
5answers
700 views

Shuffles male or females names based on Fisher Yates algorthim

The code below has many Lists<> but I'm not sure how to modify it. The only thing here that is an algorithm is the Fisher Yates shuffle algorithm. I am trying ...
5
votes
3answers
193 views

Confuse a text without making it unreadable

Scientists have found that a text can still be read even if only the first and last letter of a word are in their right place. The remaining characters within the word can be arranged randomly. ...
5
votes
2answers
912 views

Human (imperfect) array (card) shuffle written in C

I've got this humanoid_shuffle I wrote in Python. I've been wanting to learn C so as an exercise I ported it to C. As this is my first venture into C and even the ...
5
votes
2answers
279 views

Helper method to shuffle cards list for online gaming

I have written this code to shuffle a deck of cards. I would like to hear your inputs. ...
5
votes
1answer
182 views

Pile shuffle of a vector

My goal is to simulate a Pile shuffle of a vector. It takes 2 optional arguments for the number of piles to use and how many times to perform the shuffle. As this is my first attempt at clojure code, ...
5
votes
1answer
435 views

Shuffle a deck of cards, using the overhand shuffle method

I've started learning Clojure, after many years of Java. As one of a series of exercises, I'm writing a simple card game, and the overhand-shuffle function is shown ...
5
votes
2answers
4k views

Fisher-Yates shuffle algorithm in javascript, modern and inside-out versions

I've implemented two different versions of the Fisher-Yates shuffle in javascript, and I'd like to know if I've made any mistakes. I'm creating a deck of playing cards. I'm interested in these ...
4
votes
2answers
1k views

Creating multiple files from a list

This piece of code is to get random samples from two String lists, pos and neg. Based on a ...
4
votes
1answer
2k views

Tetris random piece generator

I was inspired by this Reddit /r/dailyprogrammer challenge: Contrary to popular belief, the tetromino pieces you are given in a game of Tetris are not randomly selected. Instead, all seven pieces ...
4
votes
2answers
374 views

Review of reservoir sampling

I've been studying Reservoir sampling for a couple of days. What I've tried here is draw a uniformly random sample of size 3 from bigger data (the 26 characters of the English alphabet) via reservoir ...
4
votes
1answer
1k views

Activating items in some randomized arrays

I've got a list with about 120 items in IE and I have some items I want to activate. I compare, build and randomize some Arrays on the fly. The for loop in the run()...
4
votes
3answers
1k views

Display four random entries from an array

This code will take the array shown and display 4 random entries in the format shown (or near enough). Are there any parts of this that can be optimised? I'm not happy about the ...
4
votes
1answer
90 views

getRandom() - Randomize array and get result

I've written a function that allows you to pass an array of values, which is then shuffled and returned based on a parameter $preserveArray. This is the code: <...
4
votes
2answers
1k views

Deck shuffle and drawing cards

I developed a simple deck shuffle and drawing system, as a test on how to use collections and learn from it. However, I think my code is a bit wonky, and I think here is the best place to get new ...
4
votes
1answer
62 views

Generate another random path through a complete graph

What this does is generate a random path of cities to later calculate the length of the newly generated tour of cities, the TSP. I am trying to figure out if there is a more efficient way to write ...
3
votes
2answers
4k views

Shuffle the elements of the array randomly

I have written the code to shuffle the elements of the array. Please let me know of any loopholes in the written code. ...
3
votes
2answers
3k views

Generating random numbers and decks of cards

So I have some code here: First a simple random # generator and a array-choosing function: ...
3
votes
1answer
179 views

Fisher-Yates shuffle in JavaScript

This is my first time making a function just from reading about it on Wikipedia, so I'm sure there's room for a lot of improvement. ...
3
votes
2answers
195 views

Shuffling chunks of ints in an array

The shuffle method in ShuffleMethods.java takes an array of ints and shuffles them by chunks of ints in another array. So for example, if we had an array of ints ...
3
votes
2answers
7k views

Shuffling array elements in C

This is an answer to this problem. Basically, given an array, swap the given number of elements in the array. The solution that came to my mind: since we always swap at least two elements, pick two ...
3
votes
1answer
477 views

JavaScript shuffle idea

Based on the standard implementation of Array.sort in JavaScript, would the following be a reasonable way of shuffling the contents of an array? Is it moderately ...
3
votes
1answer
59 views

POSIX + BSD-extensions implementation of shuf(1)

I implemented GNU shuf(1) in C using POSIX system calls and some BSD extensions. You must compile it with -lbsd. ...