Summary of probability problems

Source: Internet
Author: User
Tags shuffle

1. Give n cards and a random function, design a shuffle algorithm

Key points: How to ensure that each card and other probabilities appear in each position

Pseudo code is as follows

 for inch 1 ... N:     Select  from [1, I]    swap card I with card J

C + + implementation

1 voidShuffle (vector<int> Cards,intN) {2     if(Cards.empty ()) {3         return;4     }5Srand (Time (0));6      for(inti =1; I <= N; i++) {7         intj =1+ rand ()%i; 8Swap (Cards[i-1], Cards[j-1]);9     }Ten}
Shuffle

The correctness is proved by the mathematical induction method:

1. When n=1, P (probability of N cards to any position) = 1/n; established

2. Assuming that when n = k is established, then when n=k+1, we only need to determine whether the probability of satisfying each card to each position is 1/n.

Here we have to judge "each card to each position" divided into two parts, three small, to see if their probability is 1/n. That

<1> k+1 card to any location

It is known from the algorithm that when i=k+1,a position j is randomly selected from 1~ I, which is interchanged with the element I position. It can be seen that the probability of the K + 1 cards to any position is equal, that is, 1/(k +1); In the same vein,<2.1> is also proven. The probability of the first K-card to the k+1 position is also equal.

<2> Top K cards to any position

<2.1> Top K cards to k+1 position

<2.2> Top K cards to the top K positions (i.e. any card in the top K card to any position in the top K position)

The Bayesian formula, the conditional probability, is needed here.
What we are asking here is actually the probability of the index of any one of the previous K-cards x to the first K-position, if equal to 1/(k+1).
Make event A for this card x is not swapped to position k+1, event B is the card x is swapped to a position in the first K position index
Known P (b| a) = 1/k, P (a) = 1-(1/(k+1))
Then P (B) = P (b| A) *p (a) + P (b|~a) p (~a)
= 1/k * [N (1/(k+1))] + 0
= 1/(k+1)

2. How to randomly extract m number from n number in equal probability?

Follow up: How do I do if the n size is uncertain in the previous question?

3. Given an equal probability random number generator capable of generating 0, 12 numbers, "How can I generate an equal probability random number generator that generates 0,1,2,3?"

Follow up:

3.1 How do I generate rand9 with Rand7?

3.2 There are 1 coins, with the probability of P generating positive, in order to 1-p the probability of producing the back, how to use it to produce a 0.5 probability generator?

4. A,b,c Three people toss coins, the first to throw to the front of the person to win, ask three people to win the probability of how big?

Summary of probability problems

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.