Write a random shuffle function--probability problem

Source: Internet
Author: User
Tags random seed random shuffle shuffle

Title Description:

Write a random shuffle function. 52 of the required wash out! All kinds of combinations are equal probabilities. That is, the probability of a combination you wash out is 1/(52!). Assume that you have a perfect random number generator.


Problem Solving Ideas:

This is a probability problem.

Random Shuffle, the purpose is to do randomness, require each card to appear the probability of equal.

We commonly used 54 ordinary poker cards, to achieve the probability that each card appears 1/(54!),

Draw the first card probability: 1/54;

Draw the second card probability: 1/53;

Draw the third card probability: 1/52;

......

Keep it that way randomly until we get the last 1, we're going from 52! A permutation is taken out of the possibility, and the probability of the permutation is 1/(54!).

This is exactly what the topic requires, random shuffling purposes.


With the idea, the next step is how to encode the implementation.

First of all, we have a random function generator that can produce random numbers between 1-54, how to ensure that the first card drawn is 54 possible, draw the second card is 53 possible, ...

You can do this, assuming that poker is a 54-dimensional array card, all we have to do is take a random element from the array and then randomly take an element in the rest of the elements ... One problem here is that we don't let this element participate in the next selection after each element has been taken.

The goal we are going to achieve is to randomly shuffle these 54 numbers in equal probability , so that we can handle them in this way:

The first draw in the initial 54 cards, the randomly generated card x, and the first element of the interchange,

The second draw in the remaining 53 cards, the randomly generated card y, and the second element is exchanged,

......

For example, suppose 10 cards, arr[] = {1,2,3,4,5,6,7,8,9,10}, assuming that the first generation of the card is 6, then the next action is: 6 and 1 are swapped, then the remaining {2,3,4,5,1,7,8,9,10} produces a second card. ......


Reference Code:

#include <iostream> #include <cstdlib>using namespace std;void swap (int &a, int &b) {//It is possible to Swap the same variable, Cannot use XOR or version    int t = A;    A = b;    b = t;} void Swapxor (int &a, int &b)//XOR version of Interchange {       a = a^b;       b = a^b;       A = a^b;} void randomshuffle (int a[], int n) {    for (int i=0; i<n; ++i) {        int j = rand ()% (n-i) + i;//produces the random number Sw between I and N-1        AP (A[i], a[j]);//Swap position    }}int main () {    srand ((unsigned) time (0));//random seed    int n = si;    int a[] = {        1, 2, 3, 4, 5, 6, 7, 8, 9,10,......,54    };    Randomshuffle (A, n);//Call shuffle function for    (int i=0; i<n; ++i)//output one shuffle effect        cout<<a[i]<<endl;    return 0;}


Resources:

1, http://www.cricode.com/2515.html

2, http://coolshell.cn/articles/8593.html

Write a random shuffle function--probability problem

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.