简体   繁体   中英

How do I incorporate the bias values from my variables into my function? [python]

I'm simulating a scenario where there are ten pairs of socks in a laundromat, and six random socks go missing. After this, there can be at least four pairs left and at most seven. I've written a code that runs and does simulate the probability, however it treats all probabilities as equal chance when the chances aren't equal. My question is how to now incorporate the bias into this code:

import random #random number generator
# variables
number_of_pairs = 10 # how many pairs of socks there are originally
total_socks = number_of_pairs * 2 # how many total socks there are
socks_lost = 6 # how many socks were lost
number_of_simulations = 1000 # total number of trials being done
prob_of_7_pairs = (1/6) # probability that there are still seven pairs of socks left
prob_of_6_pairs = (2/9) # probability that there are still six pairs of socks left
prob_of_5_pairs = (5/18) # probability that there are still five pairs of socks left
prob_of_4_pairs = (1/3) # probability that there are still four pairs of socks left
four_pairs = 0 # how many times out of a thousand trials that there are still four pairs left
five_pairs = 0 # how many times out of a thousand trials that there are still five pairs left
six_pairs = 0 # how many times out of a thousand trials that there are still six pairs left
seven_pairs = 0 # how many times out of a thousand trials that there are still seven pairs left
# function
for i in range(0, number_of_simulations): # i is the trial runs, range is from 0 to how many trials there are
    possible_pairs = random.randint(4,7) # random number generator will randomly select between four and seven pairs of socks
    if possible_pairs == 4: 
        four_pairs += 1 # if there are only four possible pairs of socks in one of the trials, the program will add one to the total
    elif possible_pairs == 5:
        five_pairs += 1 # if there are only five possible pairs of socks in one of the trials, the program will add one to the total
    elif possible_pairs == 6:
        six_pairs += 1 # if there are only six possible pairs of socks in one of the trials, the program will add one to the total
    elif possible_pairs == 7:
        seven_pairs += 1 # if there are only seven possible pairs of socks in one of the trials, the program will add one to the total
# results
print('There were four possible pairs of socks', four_pairs, 'of times.') # prints out how many times 1 was rolled out of a thousand rolls
print('There were five possible pairs of socks', five_pairs, 'of times.') # prints out how many times 2 was rolled out of a thousand rolls
print('There were six possible pairs of socks', six_pairs, 'of times.') # prints out how many times 3 was rolled out of a thousand rolls
print('There were seven possible pairs of socks', seven_pairs, 'of times.') # prints out how many times 4 was rolled out of a thousand rolls
print('The total number of trials was', number_of_simulations,'.') # makes sure the program does a thousand trials

Let's say every stock is represented by a number between 0 - 19 (inclusive), and the pairs are stocks 0 & 1, stocks 2 & 3, etc. Then you randomly choose 6 numbers from 0 - 19 (inclusive) representing the six stocks that go missing, and calculate how many pairs have remained. Do that a lot of times (about 100,000) and find out the number of times each number comes up as the number of pairs remaining.

import random

# variables
number_of_pairs = 10
total_stocks = number_of_pairs * 2
stocks_lost = 6
number_of_simulations = 100_000
prob_of_7_pairs = (1/6)
prob_of_6_pairs = (2/9)
prob_of_5_pairs = (5/18)
prob_of_4_pairs = (1/3)
four_pairs = 0
five_pairs = 0
six_pairs = 0
seven_pairs = 0

# function
for _ in range(number_of_simulations):
    missing_stocks = set(random.sample(range(total_stocks), stocks_lost)) # generate stacks_lost unique random numbers
    possible_pairs = 0
    # loop through the pairs
    for i in range(0, total_stocks, 2):
        # if both stocks of a pair aren't missing, increment possible_pairs
        if i not in missing_stocks and i + 1 not in missing_stocks:
            possible_pairs += 1
    if possible_pairs == 4:
        four_pairs += 1
    elif possible_pairs == 5:
        five_pairs += 1
    elif possible_pairs == 6:
        six_pairs += 1
    elif possible_pairs == 7:
        seven_pairs += 1
    else:
        raise Exception("not supposed to happen")

# results
print(f'There were four possible pairs of socks {four_pairs / number_of_simulations:.2%} of the times.')
print(f'There were five possible pairs of socks {five_pairs / number_of_simulations:.2%} of the times.')
print(f'There were six possible pairs of socks {six_pairs / number_of_simulations:.2%} of the times.')
print(f'There were seven possible pairs of socks {seven_pairs / number_of_simulations:.2%} of the times.')

Output

There were four possible pairs of socks 34.71% of the times.
There were five possible pairs of socks 51.87% of the times.
There were six possible pairs of socks 13.11% of the times.
There were seven possible pairs of socks 0.31% of the times.

I don't know where you got your probabilities or how to calculate them myself but they seem to be wrong.

EDIT: added a working example, keep in mind I changed some of the variable names.
If you have any question about this code don't hesitate to ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM