Given a function that returns a random number 0 or 1, write a function that returns a number between 0 and 199
You're just trying to get a certain number of random bits, so calculate that number ahead of time:

bits = 199.to_s(2).length


Then your loop becomes:

loop do
  result = 0
  bits.times do
    result &lt;&lt;= 1
    result += f2
  end
  return result if result &gt;= 0 &amp;&amp; result &lt;= 199
end