In unbiased coin flip H or T occurs 50% of times.
But I want to simulate coin which gives H with probability 'p' and T with probability '(1-p)'.
something like this:
def flip(p): '''this function return H with probability p''' # do something return result >> [flip(0.8) for i in xrange(10)] [H,H,T,H,H,H,T,H,H,H]
Riddler Classic. Mathematician John von Neumann is credited with figuring out how to take a biased coin (whose probability of coming up heads is p, not necessarily equal to 0.5) and “simulate” a fair coin. Simply flip the coin twice. If it comes up heads both times or tails both times, then flip it twice again.
Sometimes we flip a coin, allowing chance to decide for us. But the notion that a coin flip is random and gives a 50-50 chance of either heads or tails is, unfortunately, fallacious. That's because the mechanics that govern coin flips are predictable.
random.random() returns a uniformly distributed pseudo-random floating point number in the range [0, 1). This number is less than a given number p in the range [0,1) with probability p. Thus:
def flip(p): return 'H' if random.random() < p else 'T' Some experiments:
>>> N = 100 >>> flips = [flip(0.2) for i in xrange(N)] >>> float(flips.count('H'))/N 0.17999999999999999 # Approximately 20% of the coins are heads >>> N = 10000 >>> flips = [flip(0.2) for i in xrange(N)] >>> float(flips.count('H'))/N 0.20549999999999999 # Better approximation
Do you want the "bias" to be based in symmetric distribuition? Or maybe exponential distribution? Gaussian anyone?
Well, here are all the methods, extracted from random documentation itself.
First, an example of triangular distribution:
print random.triangular(0, 1, 0.7)
random.triangular(low, high, mode):Return a random floating point number
Nsuch thatlow <= N < highand with the specified mode between those bounds. Thelowandhighbounds default to zero and one. Themodeargument defaults to the midpoint between the bounds, giving a symmetric distribution.
random.betavariate(alpha, beta):Beta distribution. Conditions on the parameters are
alpha > 0andbeta > 0. Returned values range between0and1.
random.expovariate(lambd):Exponential distribution.
lambdis1.0divided by the desired mean. It should be nonzero. (The parameter would be called “lambda”, but that is a reserved word in Python.) Returned values range from0to positive infinity iflambdis positive, and from negative infinity to0iflambdis negative.
random.gammavariate(alpha, beta):Gamma distribution. (Not the gamma function!) Conditions on the parameters are
alpha > 0andbeta > 0.
random.gauss(mu, sigma):Gaussian distribution.
muis the mean, andsigmais the standard deviation. This is slightly faster than thenormalvariate()function defined below.
random.lognormvariate(mu, sigma):Log normal distribution. If you take the natural logarithm of this distribution, you’ll get a normal distribution with mean
muand standard deviationsigma.mucan have any value, andsigmamust be greater than zero.
random.normalvariate(mu, sigma):Normal distribution.
muis the mean, andsigmais the standard deviation.
random.vonmisesvariate(mu, kappa):
muis the mean angle, expressed in radians between0and2*pi, andkappais the concentration parameter, which must be greater than or equal to zero. Ifkappais equal to zero, this distribution reduces to a uniform random angle over the range0to2*pi.
random.paretovariate(alpha):Pareto distribution.
alphais the shape parameter.
random.weibullvariate(alpha, beta)Weibull distribution.
alphais the scale parameter andbetais the shape parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With