class NormalFromUniform
  
  def initialize()
    @next = nil
  end
  
  def rand()
    if @next
      retval, @next = @next, nil
      return retval
    else
      u = v = s = nil
      loop do
        u = Random.rand(-1.0..1.0)
        v = Random.rand(-1.0..1.0)
        s = u**2 + v**2
        break if (s > 0.0) && (s <= 1.0)
      end
      f = Math.sqrt(-2.0 * Math.log(s) / s)
      @next = v * f
      return u * f
    end
  end
end