Quantcast
Viewing all articles
Browse latest Browse all 11

Answer by Yavuz Koroglu for Why do people say there is modulo bias when using a random number generator?

I just wrote a code for Von Neumann's Unbiased Coin Flip Method, that should theoretically eliminate any bias in the random number generation process. More info can be found at (http://en.wikipedia.org/wiki/Fair_coin)

int unbiased_random_bit() {        int x1, x2, prev;    prev = 2;    x1 = rand() % 2;    x2 = rand() % 2;    for (;; x1 = rand() % 2, x2 = rand() % 2)    {        if (x1 ^ x2)      // 01 -> 1, or 10 -> 0.        {            return x2;                }        else if (x1 & x2)        {            if (!prev)    // 0011                return 1;            else                prev = 1; // 1111 -> continue, bias unresolved        }        else        {            if (prev == 1)// 1100                return 0;            else          // 0000 -> continue, bias unresolved                prev = 0;        }    }}

Viewing all articles
Browse latest Browse all 11

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>