Class: NoSE::RandomGaussian

Inherits:
Object show all
Defined in:
lib/nose/random.rb

Overview

Generate random numbers according to a Guassian distribution

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mean, stddev, integer = true, min = 1) ⇒ RandomGaussian

Returns a new instance of RandomGaussian.



346
347
348
349
350
351
352
353
# 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

Class Method Details

.gaussian(mean, stddev) ⇒ Array<Fixnum>

Return a random number for the given distribution

Returns:



371
372
373
374
375
376
377
378
# 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

Instance Method Details

#randFixnum

Return the next valid random number

Returns:

  • (Fixnum)


357
358
359
360
361
362
363
364
365
366
367
# 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