The practice, like everyone else, has been understood,ran = Random.random (), is a random floating-point number that produces between 0 ——— 1,
such as the next code, the first randomly generated number less than 0.1 probability is 10%, if the first number is greater than 0.1 is the probability of the first take and after the problem
ImportRandomdefSelect (): Num_= ['a','b','C'] #probability list r_ = [0.1, 0.3, 0.6 ] sum_=0 ran=random.random () forNum, RinchZip (num_, r_): Sum_+=RifRan < sum_: Break returnNum"""verification results, multiple cycles to count the number of occurrences of a,b,c alone, to determine the corresponding probability"""num_a, Num_b, Num_c=0, 0, 0 foreachinchRange (100000): Result=Select ()ifresult = ='a': Num_a+ = 1elifresult = ='b': Num_b+ = 1Else: Num_c+ = 1Print(Num_a, Num_b, Num_c)
The results are as expected:
9983 29973 60044
Select the corresponding element with the probability list, and choose the python for the probability of roulette