A Binary Random Number Generator

Although there is nothing random involved in principle, here is one way to produce a Bernoulli random variable $X$ with $P_X(0) = P_X(1) = 1/2$. We will use the 'clock' command of Matlab. 'clock' returns a vector.

c = clock
c =

   1.0e+03 *

    2.0150    0.0030    0.0260    0.0150    0.0290    0.0002

The last component gives the second upto a few decimal places. We look at a small enough digit and check if it is even or odd.

x = mod( round( c(6) * 10^3 ), 2 )
x =

     1

To check if $X$ is indeed uniformly distributed, we perform an experiment as follows. We repeat the experiment above many times and check how many ones we obtain. Roughly the half of the realizations should be one.

s = 0; % this will hold the sum of the realizations.
N = 10^3; % we repeat the experiment N times
for n = 1:N,
    c = clock;
    x = mod( round( c(6) * 10^3 ), 2 );
    pause( 0.001 * ( c(6) ) ); % pause a little
    s = s + x;
end

s/N % the answer should be close to 1/2.
ans =

    0.4980

Using independent Bernoulli random variables, we can in principle produce any discrete and/or continuous random variable (if we are allowed to use infinitely many random variables). Can you construct a discrete random variable with PMF uniform over the the integers between $[0,N]$ as a function of Bernoulli random variables?

Ilker Bayram, Istanbul Teknik Universitesi, 2015.