A simple and effective shuffle algorithm

Source: Internet
Author: User
Tags generator random shuffle shuffle

Assembly head brother in a post to point out an interesting shuffle algorithm, bloggers according to his ideas to write another shuffle algorithm. The following is the idea of the shuffle algorithm:

Let's take a look at the card game first. A card consists of 52 different cards, cards must be issued without repetition of the card, and the shuffle process must be fair, that is 52! The order of cards should appear in the same probability. It is obvious that the random numbers produced by this random permutation must be evenly distributed and independent. This code is as follows:

using System;
using System.Diagnostics;

Namespace Lucifer.CSharp.Sample
{
Class program
{
static void Main (string[] args)
{
//initialization hand int[] array = new INT[52];
for (int i = 0; i < array. Length; i++)
{
Array[i] = i;
}

//Shuffle
Permute<int> (array);

//Authentication hand
for (int i = 0; i < array. Length; i++)
{
var value = Array[i];
for (int j = 0; J < array.) Length; J + +)
{
if (j = = i) Continue
Debug.Assert (Array[j]!= value);  
}
}
}

static void Permute<t> (t[] array)
{
Random Random = new Random ();
for (int i = 1; I < array. Length; i++)
{
swap<t> (array, I, random). Next (0, i));
}
}

static void Swap<t> (t[] array, int indexa, int indexb)
{
T temp = Array[indexa];
A Rray[indexa] = Array[indexb];
Array[indexb] = temp;
}
}
}

The permute<t> (t[] array) method in the code example produces a random sequence. The first loop uses 1, 2, 3, ..., N to initialize the sequence. The second loop completes a random shuffle. In each iteration of the loop, we say that the value of Array[j] is exchanged (or not exchanged) for an element in the array position between the interval [0, J).

However, what we are asking is the probability of all permutations generated by the permute<t> (t[] array) method.

According to the algorithm, the answer is yes. Because a total of N! Possible permutations, while swap<t> (array, I, random). Next (0, i)); This sentence N-1 the different result of the call Next method is also N! Two But, in fact, the answer is no, not all permutations are equal probabilities. The problem is on the cute pseudo-random number generator (pseudo-random numbers generator). The randomness of the PRNG largely limits the randomness of random sequences. Therefore, the above code needs a better pseudo-random number generator to make the actual and the theory more consistent. But good PRNG are often accompanied by decreased performance, such as Mt19937 randomization algorithms.

Digression: Bloggers in the actual code run, found that the code can be very good to complete the shuffle algorithm requirements, but do not guarantee that it can be in the commercialization of the project to properly practice. I remember brother Yunfeng on his blog has also discussed the shuffle algorithm, interested brothers can search for some.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.