Questions tagged [fisher-yates-shuffle]

The Fisher–Yates shuffle is an algorithm for generating a random permutation of a finite sequence. Use this tag for questions about this algorithm and it's implementation.

34 questions
18
votes
1 answer

Bug in .Net's `Random` class?

I was looking at a question that was talking about a bad implementation of the Fisher-Yates shuffling algorithm and I was perplexed that there was a bias when implemented incorrectly. The two algorithms are these: private Random _random = new…
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
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
4
votes
3 answers

A variant of Knuth shuffle

This is a very hard but interesting probability question related to Knuth/Fisher-Yates shuffle. When looping for each element, the swap is performed for the current element with any random element from the whole array (not within the elements left),…
essence16
  • 163
  • 11
2
votes
1 answer

Unbiased Shuffling with Large Number of Duplicates

The Fisher–Yates algorithm generates unbiased random permutations of a finite sequence. The running time is proportional to the number elements being shuffled. I want to shuffle a few non-zero elements with a large number of zero…
2
votes
4 answers

How to write a prioritized left-shuffle algorithm in O(n)?

There are shuffle algorithms like FisherYates. They take an array and return one with elements in random order. This runs in O(n). What I'm trying to do is to implement a prioritized left-shuffle algorithm. What does that mean? Prioritized: It does…
hardfork
  • 2,470
  • 1
  • 23
  • 43
1
vote
1 answer

Math.random() unsatisfactory

Let be a list containing the 26 letters of the alphabet from A to Z. We mix the list with the Fisher-Yates algorithm (see https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle). We are interested in the first letter of the table : In theory,…
user3515941
  • 141
  • 1
  • 8
1
vote
1 answer

Unbiased random Array shuffle

When I want to shuffle an array of numbers in an array "perm" from [1...n], I wrote in Java: int[] perm = new int[n]; for (int i = 0; i < n; i++) { perm[i] = i; } for (int i = 0; i < n; i++) { int new_place = (int) (Math.random()*n); //…
Kushal Kumar
  • 174
  • 1
  • 13
1
vote
1 answer

Replace commas with spaces ? Fisher-Yates randomization

Thanks to @axtck for the help for the Fisher Yates randomization, he helped me to change number into words here : Since the shuffle functions shuffle the arrays indexes, you can just shuffle the array the same way you did but add name strings in the…
1
vote
0 answers

Alter Fisher-Yates shuffle so that a value never ends up in the same place it started

I have an array of objects, a sample of which looks like this: var allWorm = [ { "name": "Null", "power": "Create an artificial cluster. Everyone added to this cluster has their parahuman abilities shared, but with a…
1
vote
1 answer

Fisher yates algorithm is not yielding unbiased results

Fisher yates algorithm as described on wikipedia is The algorithm produces an unbiased permutation: every permutation is equally likely. I went through some articles that explains how a naive and fisher yates algorithm can produce biased and…
f_i
  • 3,084
  • 28
  • 31
1
vote
1 answer

How do I shuffle an array in TypeScript with the Fisher-Yates Shuffle?

I'm trying to code a quiz with TypeScript. The questions that are asked should be picked randomly and I'm using the Fisher-Yates Shuffle to do that – but it won't shuffle them. What am I doing wrong? The Fisher-Yates Shuffle appears after the…
0
votes
0 answers

array randomization accuracy of the decision tree vs fisher yates shuffle algorithm

now I want to know about the accuracy of the randomization of the decision tree and fisher yates algorithms. if the two algorithms are compared how do i find out? I tried to find references about the comparison of the two algorithms but couldn't…
0
votes
0 answers

command shuffle not worked in android studio

I am a beginner in java programming language I tried to implement Fisher Yates's algorithm in my code, but when I run the application it doesn't randomize my puzzle. when I was looking for references I was confused about the correct location to…
Fatkul
  • 1
  • 2
0
votes
0 answers

How to shuffle an array that has duplicate elements in C, such that the duplicates are separated by a spacing of 2? I am using the FisherYates Shuffle

#include #include #include #include int main(void){ int num[] = {1, 1, 2, 2, 3, 3}; // Given array which has to be shuffled int swap; int i, j; srand(time(NULL)); for(i = 1; i < 6; i++){ // Start…
0
votes
1 answer

How can I update state in a component when the same state is passed as props in another component?

(component A is Home.jsx, component B is ChartSongs.jsx and component C is AudioPlayer.jsx)....... In component A I have an array of songs,then I passed it as props to component B {/*The array*/} chartData whose value was fetched from an…
Tomiwa
  • 59
  • 7
1
2 3