import random


def biased_getrandbits(p, k=8):
    """Return an integer where bits are true with probability p.

p -- probability of each bit being set, 0 < p < 1
k -- number of bits in byte

If p == 0.5, this behaves the same as random.getrandbits().
"""
    b = 0
    for i in range(k):
        b = b | (1 << i if (random.random() < p) else 0)
    return b