See also Monte Carlo--python simulation solves Sanmen problem

Source: Internet
Author: User

The question is very interesting, the wiki in different ways to explain the principle is very thorough, I like the understanding of the way: regardless of the participants to start the choice, in the moderator asked if the change is the choice of replacement. If the contestant selects the goat first, it will win after the change, and if the contestant selects the car first, the 100% loses after the change. The probability of selecting a goat is 2/3, and the probability of selecting a car is 1/3. So in any case, the conversion option can increase the chances of winning compared to the initial 1/3 chance of winning the car.

The principle is clear, the implementation is relatively simple, this time with Python.

Import random as Rndstrategy = [' stick ', ' choose ', ' Swith ']def MC (strategy,times): Wins = 0for Trail in range: # assuming, actually Prizes at Gate No. 0 ... But we don't know ... envelops = [0,1,2]# first randomly selects a door First_choice = Rnd.choice (envelops) # The second strategy faces two different combinations of options depending on the first choice: If gate No. 0 is chosen for the first time, then after opening one of the other two doors, the Stargate will be selected for the second time in gate No. 0 and the open Stargate (1 or 2) if First_choice = = 0:envelops = [0,rnd.choice]]# If 0 is not selected for the first time, then it must be another Stargate at this time, then # in the second choice, will be in 0 and their own now in the door (First_choice) to make a choice else:envelops = [0,first_choice]# Take a different strategy for the second choice # keep the original position unchanged if strategy = = ' stick ': Second_choice = first_choice# in the two gates after removing a Stargate, randomly select a elif strategy = = ' Choose ': Second_choice = Rnd.choice (envelops) # Excluding a Stargate, discard the original selection, directly select another door elif strategy = ' switch ': Envelops.remove ( First_choice) Second_choice = envelops[0]# Remember, the prize at Gate No. 0 if Second_choice = = 0:wins + = # calculate the winning probability value P = wins/timesprint (' Second choice adopted ' +strategy+ ' method, the probability of winning is: ' +str (P) + ' (the number of simulations is ' +str (times) + ') ') MC (' Stick ', 10000) MC (' Choose ', 10000) MC (' Switch ', 10000)

The output is as follows:

wonderful!

See also Monte Carlo--python simulation solves Sanmen 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.