Sha256: 8e7ca97f0ea5bf87218d232cb08d89bae0c6a17e67ecb47800cc7cac619866b1

Contents?: true

Size: 771 Bytes

Versions: 3

Compression:

Stored size: 771 Bytes

Contents

module RubyPager

  class GaussianNoise
    def initialize(ex_mean, ex_stddev, ex_rand_helper = lambda { Kernel.rand })
      @rand_helper = ex_rand_helper
      @mean = ex_mean
      @stddev = ex_stddev
      @valid = false
      @next = 0
    end

    def rand
      if @valid then
        @valid = false
        return @next
      else
        @valid = true
        x, y = self.class.gaussian(@mean, @stddev, @rand_helper)
        @next = y
        return x
      end
    end

    private
    def self.gaussian(mean, stddev, rand)
      theta = 2 * Math::PI * rand.call
      rho = Math.sqrt(-2 * Math.log(1 - rand.call))
      scale = stddev * rho
      x = mean + scale * Math.cos(theta)
      y = mean + scale * Math.sin(theta)
      return x, y
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby_pager-0.1.3 lib/ruby_pager/gaussian_noise.rb
ruby_pager-0.1.2 lib/ruby_pager/gaussian_noise.rb
ruby_pager-0.1.1 lib/ruby_pager/gaussian_noise.rb