probability

Distribute discount coupons evenly between new and old users

Distribute discount coupons evenly between new and old users Question: I recently got this problem in an interview. Suppose you have 3 types of coupon: Free Shipping( To be distributed to 10% users) By one Get One (To be distributes to 10% users) Flat 10% off (To be distributed to 80%) The tasks is to …

Total answers: 1

Trying to generate a conditional coin flip

Trying to generate a conditional coin flip Question: So I’m a trying to create a function which first flips an unbiased coin but if the result is heads it flips a biased coin with 0.75 probability of heads. If it shows tails then the next flip is unbiased. I’ve tried the following code but I …

Total answers: 3

100 Prisoners Dilemma – Python code isn't working

100 Prisoners Dilemma – Python code isn't working Question: I stumbled across an interesting probability problem about half an hour ago. https://en.wikipedia.org/wiki/100_prisoners_problem Essentially, there are 100 boxes, drawers, etc, each with a unique number between 1-100 inside of them. There are also 100 prisoners. Each prisoner has 50 chances to find the box with their …

Total answers: 2

Why does random.choices with weights don't match my expectation?

Why does random.choices with weights don't match my expectation? Question: When I execute this code, I expect both functions to return roughly 0.6. Yet, always_sunny returns approx. 0.6 as expected, but guess_with_weight returns approx. 0.52 instead. import random k = 1000000 def sample(): return random.choices(population=[‘rain’, ‘sunny’], weights=[0.4, 0.6], k=k) def always_sunny(xs): return sum( [ (1 …

Total answers: 1

Swapping consecutive element in a list with a probability in Python

Swapping consecutive element in a list with a probability in Python Question: I want to swap consecutive elements in list with a probability. For example, I have this list: l=[1,2,3,4,5,6,7,8,9,10,11] I wrote the following code thats swap the consecutive elements length=len(l) if len(l)%2==0 else len(l)-1 for i in range(0,length,2): l[i],l[i+1]=l[i+1],l[I] The above code results in …

Total answers: 1

Probability of substring within string – this is being performed in python

Probability of substring within string – this is being performed in python Question: I have a script that matches IDs in DF1 to DF2, in some cases the matches are completed via matching a string from DF1 as a substring to DF2. For example: DF1 ID: 2abc1 DF2 ID: 32abc13d The match in the above …

Total answers: 1

Point x with higher density than y has lower probability than y

Point x with higher density than y has lower probability than y Question: I am using beta distribution to model my problem I optimize the curve using a and b parameters and I get the outcome I want, but when I try to calculate the probability of two points using small intervals to compute the …

Total answers: 1

Rolling N Non-regular Dice in Constant Time

Rolling N Non-regular Dice in Constant Time Question: Say I have a non-regular dice defined by probabilities in a list that add up to one, e.g [0.1, 0.3, 0.4, 0.2]. I can use the following code to simulate rolling that dice n times: import random from collections import Counter def roll(dist, n): sides = [i …

Total answers: 1