Sha256: 11cdd5c40535df5f4a29abfeba85ccb65e6d801ef15f317d969bce3d5fa91212

Contents?: true

Size: 687 Bytes

Versions: 37

Compression:

Stored size: 687 Bytes

Contents

# http://creativecommons.org/publicdomain/zero/1.0/
class RandomGaussian
  def initialize(mean, stddev, rand_helper = -> { Kernel.rand })
    @rand_helper = rand_helper
    @mean = mean
    @stddev = stddev
    @valid = false
    @next = 0
  end

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

  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)
    [x, y]
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
tensor_stream-1.0.9 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-1.0.8 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-1.0.7 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-1.0.6 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-1.0.5 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-1.0.4 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-1.0.3 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-1.0.2 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-1.0.1 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-1.0.0 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-1.0.0.pre.rc1 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-0.9.10 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-0.9.9 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-0.9.8 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-0.9.7 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-0.9.6 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-0.9.5 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-0.9.2 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-0.9.1 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
tensor_stream-0.9.0 lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb