Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Random bit with parameterized bias

By: fgrieu on Mar 23rd, 2013  |  syntax: C  |  size: 0.39 KB  |  hits: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // given get_random_bit() returning unbiased 0 or 1,
  2. // return 0 or 1 with specified bias (odds of a 1 is bias)
  3. // credit: Poncho's http://crypto.stackexchange.com/a/6508/555
  4. // 2013-03-23 modified to recover the original's numerical stability
  5. int biased_bit(double bias) {
  6.     while (bias>0) {
  7.         if (bias >= 1) return 1;
  8.         bias = 2*bias-get_random_bit();
  9.     }
  10.     return 0;
  11. }