In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you an example analysis of the problem of Python probability generation. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
In the problem of probability generation, there is an uneven coin, which requires a uniform probability distribution and a non-uniform probability distribution, such as 0.25,0.75 using Rand7 () to achieve the same probability of Rand10 () uneven coins.
There is an uneven coin coin (), which can return two values of 0 and 1, and their probabilities are 0.6,0.4 respectively. It is required to use this coin to produce a uniform probability distribution. That is, the probability of writing a function coin_new () to return 0 and 1 is 0. 5.
# uneven coins, the probability of returning 0 and 1 is 0. 6, 0.4def coin (): return 0 if random.randint (1 else 10) > 4 else 1
Statistics on the probability distribution of the results of two coin tosses:
Result 0 1
0 0.6 0 6 0 36 0 6 0 4 1 0 24
1 0.40.6 0.24 0.40.4 0.16
The probability distributions of 0 1 and 1 0 obtained by flipping two coins in succession are the same. So the solution to this problem is to flip a coin twice in a row, return 0 if you get 0 1, return 1 if you get 1 0, and flip it again if the results are the same twice.
By analogy, no matter what the probability of this uneven coin is, the result of equal probability can be obtained by this method.
Ddef coin_new (): while True: a = coin () if coin ()! = a: return
Complete test code:
Def coin (): return 0 if random.randint (1Power10) > 4 else 1def coin_new (): while True: a = coin () if coin ()! = a: return aif _ name__ = ='_ main__': a = 0b = 0n = 100000 for _ in range (n): if coin_new (): a + = 1 if coin (): B + = 1 print (f "1: {Azone}) 1: {bplan} ") uniform coins produce unequal probabilities
There is a uniform coin coin (), which can return two values of 0 and 1, with a probability of 0.5. It is required to write a function coin_new () that returns the specified 0, 1 probability distribution.
# uniform coin def coin (): return random.randint (0penny 1)
P (0) = 1 stroke 4 ~ 4 P (1) = 3 ~ 4
For uniform coins, the probabilities of 0 0, 0 1, 1 0 and 1 1 are all 1 hand 4 when tossed twice in a row. Obviously, you only need to flip the coin twice in a row, and if you get 0, it returns 0, and in other cases it returns 1.
Def coin_new (): return coin () or coin ()
P (0) = 1pm 3pm P (1) = 2pm 3
Flip a coin twice in a row. If you get 1 1, return 0; if you get 1 0 or 0 1, return 1; if you get 0 0, continue to flip a coin.
Def coin_new (): while True: a, b = coin (), coin () if a & b: return 0 if a | b: return 1
P (0) = 0.3, P (1) = 0.7
Every time you flip a coin, you get a bit of a binary number. If you flip a coin four times in a row, you can generate each number of [0,15] with equal probability, marked as x. If you remove [10,15], every number of the remaining [0,9] is still equal probability. If x ∈ [0≥ 2] x\ in [0L2] x ∈ [0L2], return 0; x ∈ [4LJ 9] x in [4LJ 9] x ∈ [4LJ 9], return 1; x ≥ 10, repeat the above process.
Def coin_new (): while True: X = 0 for _ in range (4): X = (x
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.