class NoSE::RandomGaussian

Generate random numbers according to a Guassian distribution

Public Class Methods

gaussian(mean, stddev) click to toggle source

Return a random number for the given distribution @return [Array<Integer>]

# File lib/nose/random.rb, line 371
def self.gaussian(mean, stddev)
  theta = 2 * Math::PI * rand
  rho = Math.sqrt(-2 * Math.log(1 - rand))
  scale = stddev * rho
  x = mean + scale * Math.cos(theta)
  y = mean + scale * Math.sin(theta)
  [x, y]
end
new(mean, stddev, integer = true, min = 1) click to toggle source
# File lib/nose/random.rb, line 346
def initialize(mean, stddev, integer = true, min = 1)
  @mean = mean
  @stddev = stddev
  @valid = false
  @next = 0
  @integer = integer
  @min = min
end

Public Instance Methods

rand() click to toggle source

Return the next valid random number @return [Integer]

# File lib/nose/random.rb, line 357
def rand
  if @valid
    @valid = false
    clamp @next
  else
    @valid = true
    x, y = self.class.gaussian(@mean, @stddev)
    @next = y
    clamp x
  end
end

Private Instance Methods

clamp(value) click to toggle source

Clamp the value to the given minimum

# File lib/nose/random.rb, line 383
def clamp(value)
  value = value.to_i if @integer
  [@min, value].max unless @min.nil?
end