Uniform random number generation from random bits
First we need reduce our rand(from, to) function just to rand(n) = returns uniformly random number from [0, n); and later we will be able construct original function as rand(from, to) = rand(to - from) + to.


  $rand(n)=$
  
  
  Take $k$ random bits such where $n \le 2 ^ k$ (actually we can choose any $k$ that satisfy this condition, but in practice make sense to choose minimal among all possible $k$) and interpret this sequence as binary integer $r$.
  If $r &lt; n$ return $r$ else go to step 1.
  


Now let's calculate probability that some number $r$ will be choosen:


Probability that algorithm will stop after one iteration is $\frac{n}{m}$, probability that we will reach 2nd iteration is $\frac{m-n}{m}$; more generally probability that algorithm will reach $step$ interation is $Pr(step) = (\frac{m-n}{m}) ^ {step - 1}$.
Probabilities that algorithm will return some $r$ on 1st iteration:


$
Pr(0) = \frac{1}{m}\\
Pr(1) = \frac{1}{m}\\
Pr(2) = \frac{1}{m}\\
...\\
Pr(n - 1) = \frac{1}{m}\\
$


Probabilities that algorithm will return some $r$ on 2nd iteration:


$
Pr(0) = \frac{m-n}{m}*\frac{1}{m}\\
Pr(1) = \frac{m-n}{m}*\frac{1}{m}\\
Pr(2) = \frac{m-n}{m}*\frac{1}{m}\\
...\\
Pr(n - 1) = \frac{m-n}{m}*\frac{1}{m}\\
$


Probabilities that algorithm will return some $r$ on $step$ iteration:


$
Pr(0) = (\frac{m-n}{m}) ^ {step - 1}*\frac{1}{m}\\
Pr(1) = (\frac{m-n}{m}) ^ {step - 1}*\frac{1}{m}\\
Pr(2) = (\frac{m-n}{m}) ^ {step - 1}*\frac{1}{m}\\
...\\
Pr(n - 1) = (\frac{m-n}{m}) ^ {step - 1}*\frac{1}{m}\\
$


And probability that some $r$ will be choosen (after any number of interations) will be the same regardless of $r$:


$
Pr(r) = \frac{1}{m} + \frac{m-n}{m}*\frac{1}{m} + ... + (\frac{m-n}{m}) ^ s*\frac{1}{m} + ... = \frac{1}{m}\sum\limits_{s=0}^\infty(\frac{m-n}{m}) ^ s = \frac{1}{m} \frac{1}{1 - \frac{m - n}{m}} = \frac{1}{m} \frac{m}{n} = \frac{1}{n}
$