Classical probability problems such as the probability of the number/reservoir problem, etc.

Source: Internet
Author: User

Title: To a deck of cards and a random number function, design a shuffle algorithm.

Analytical:
What is the most intuitive way of thinking? It's easy to get a random one out of a stack of cards at a time. So, the first time there are 52 possible, after the end of the 51, the second take 51 possible, the third to take 50 kinds of possibilities, ..., have been so randomly held until the last 1, we will be 52! The probability of this permutation is 1/(52!), That's exactly what the subject requires.

The next question is, how do you write code to implement the algorithm above? Assuming that poker is a 52-dimensional array cards, 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. How is this going to be done?

Let's first assume a 5-dimensional array: 1,2,3,4,5. If the 1th random number is 4, then we want to participate in the 2nd random selection of only 1,2,3,5. Since 4 has not been used, we can swap it with 1, and for the 2nd time we just need to randomly select it from the back 4 bits (2,3,1,5). Similarly, the 2nd randomly selected element and the 2nd element in the array are exchanged, and then the elements are randomly selected from the next 3 elements, and so on.
Code:

#include <iostream>#include<cstdlib>using namespacestd;voidSwap (int&a,int&AMP;B) {//it is possible to swap the same variable, not with XOR version    intt =A; A=b; b=t;}voidRandomshuffle (intA[],intN) {     for(intI=0; i<n; ++i) {        intj = rand ()% (n-i) + i;//generate random numbers between I and N-1Swap (A[i], a[j]); }}intMain () {srand (unsigned) time (0)); intn =9; intA[] = {        1,2,3,4,5,6,7,8,9    };    Randomshuffle (A, n);  for(intI=0; i<n; ++i) cout<<a[i]<<Endl; return 0;}

Handout Tutorial Version:

For I in 1...N

Randomly select a card j from [1, I]

Swap card I with card J

Prove:

We make use of mathematical inductive method to prove the correctness of algorithm 1:

Equal probability of each card appearing in each position (1/n)

When n = 1 o'clock, it becomes apparent that

When n=2, the probability of each card appearing in two positions is 1/2.

Assuming that when n=k, we now prove that n = k +1 is also the time to stand, that is, each card appears in each position of the probability of 1/(k + 1)

We are divided into three parts: the k+1 card to all positions, the first k cards to the k+1 position, the first K cards to the first K positions

Section k+1 card to all locations
Obviously, the probability of K + 1 cards to all positions is 1/(k + 1), which is what the algorithm says:)

First k card to k+1 position

In the same vein, the probability of the former K-card being exchanged to K + a position is 1/(k + 1)

Top K cards to the first K positions (most complex):
We know that n = k is the time to stand, so do not consider the first k+1 card, the probability of the former K card in the first k position is 1/k

But now, because of the appearance of the k+1 card, we have to make sure that one of the top K cards is not exchanged? Go, this probability is: (1-1/(k + 1))

So according to the Bayesian formula, the final probability is:
1/k * (1-1/(k + 1)) = 1/(k + 1)

Classical probability problems such as the probability of the number/reservoir problem, etc.

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.