Common functions in random modules
Copy Code code as follows:
Random ()
Returns a real number in the interval [0,1];
Uniform (A, B)
Returns a real number in the interval [a,b];
Randint (A, B)
Returns an integer in the interval [a,b];
Choice (sequence)
Returns an element in sequence where the sequence is an ordered sequence, such as a list, string, or tuple;
Randrange ([start], stop[, step])
Equivalent to Choice (range ([start], stop[, step]);
Shuffle (sequence [, Random])
No return value is used to disrupt the order of elements in the sequence;
sample (sequence, n)
Returns a fragment consisting of elements in n sequence, where sequence can also be a set type.
Using Itertools to get permutations and combinations
Copy Code code as follows:
Permutations (sequence, k))
All permutations containing k elements are obtained from the sequence sequence.
Combinations (sequence, k))
All the combinations containing K elements are obtained from the sequence sequence.
Sheep door problem
There is a lottery show, there are three closed doors, a car behind the door, the rest of the door is a goat, only the host know what behind each door. Participants can choose a door, before opening it, the host will open another door, exposing the goat behind the door, and then allow the contestants to change their choice. The question is: Will the contestants be able to increase their chances of winning the car when they change their options?
There are many times when we do not know whether our theoretical analysis is correct or not, but if we know the law of large numbers in probability theory, and we happen to know a little bit of programming, we can no doubt use the computer to repeat the simulation events to solve the problem. The Python 3.x solution for this problem is as follows:
Copy Code code as follows:
From random Import *
def once (doors = 3): # Simulation of an event
Car = randrange (doors) # One door is parked behind the bus
Man = Randrange (doors) # Contestants choose a door in advance
return car = Man # did the contestants choose to be in the first place?
h = 0 # Insist on choosing the number of times to win a car
c = 0 # Change the number of times you choose to win a car
times = Int (1e6) # Repeat the number of experiments
For I in Range (times):
If once (): H + 1
ELSE:C + 1
Print ("Maintain selection:", h/times*100, "%\n Change Selection:", c/times*100, "%")
Run Result:
Maintenance option: 33.268
Change the choice: 66.732%
Poker problem
Probability theory brings us a lot of strange and unusual results, especially in terms of conditional probabilities. Such as:
Four people playing poker, one of them said, I have a a on hand. What is the probability of having more than one a on his hand?
Four people playing poker, one of them said, I have a spade in my hand. What is the probability of having more than one a on his hand?
Copy Code code as follows:
From random Import *
cards = [I for I in range (52)]
Counter = [0, 0, 0, 0]
def once (): # 0 means spades A
Global cards
Ace = Set (sample cards) & {0,1,2,3}
Return Len (ACE), 0 in Ace
For I in range (int (1e6)):
A, S = once () # A indicates the number of a, s indicates whether there is a spade a
If a:
COUNTER[1] + + 1
If S:COUNTER[3] + + 1
If a > 1:
COUNTER[0] + + 1
If S:COUNTER[2] + + 1
Print (' Case one: ', counter[0]/counter[1], ' \ n situation two: ', counter[2]/counter[3]
Run Result:
Situation One: 0.3694922900321386
Situation Two: 0.5613778028656186
The interesting thing came out: if the man announced the suit of a in his hand, the probability of holding a plurality of a in his hand would be greatly increased. But how should this be understood?
One family has two children, one of whom is known to be a girl, and the probability of another child being a girl.
On the internet every time someone posted a paradox related to conditional probability, there will always be countless people watching and arguing, even if the essence of these problems are the same. This topic is undoubtedly one of the most contentious issues.
said that the analysis on the Internet are all in the right, some of the original confused people are said to be bewildered, a feeling that this pair, and then feel that the right. Now that I don't give you an analysis of that, use a computer to simulate the problem so you can get a straight conclusion without having to understand why.
Copy Code code as follows:
From random Import * # 0 means girl, 1 means boy
Family = (Lambda n: [{randrange (2), Randrange (2)} for I in range (n)]) (int (1e6))
both = Family.count ({0}) # are the number of girls ' families
exist = Len (family)-Family.count ({1}) # Number of families with girls
Print (both/exist)
Run Result:
Copy Code code as follows:
Without those esoteric analysis processes, a few lines of code get the answer to the question, presumably the benefit of the computer's introduction of mathematical calculations and proofs.
Birthday paradox
Everyone has a birthday, occasionally meet with their own birthday on the same day, but in life this fate does not seem to be often. Let's guess: What are the odds of this fate among 50 people, 10%, 20% or 50%?
Copy Code code as follows:
From random Import *
Counter, times = 0, int (1e6)
For I in Range (times):
If Len ({randrange (365) for I in range (50)})!= 50: # The one with the same birthday
Counter + 1
Print (' The probability of having the same birthday in 50 persons is: ', counter/times)
Run Result:
Copy Code code as follows:
The probability of having the same birthday in 50 persons is: 0.970109
The probability of having the same birthday in 50 is as high as 97%, which is probably more likely than most people expect. We are not mistaken, our intuition is wrong, science and life again a joke. Precisely because the results of the calculation and day-to-day experience produced so obvious contradictions, the problem is called "Birthday paradox", it embodies the rational calculation and perceptual knowledge of the contradictions, does not cause logical contradictions, so it is not a strict sense of the paradox.