// given get_random_bit() returning unbiased 0 or 1,
// return 0 or 1 with specified bias (odds of a 1 is bias)
// credit: Poncho's http://crypto.stackexchange.com/a/6508/555
// 2013-03-23 modified to recover the original's numerical stability
int biased_bit(double bias) {
while (bias>0) {
if (bias >= 1) return 1;
bias = 2*bias-get_random_bit();
}
return 0;
}