Class: RandomSet::Gaussian

Inherits:
Object
  • Object
show all
Defined in:
lib/random_set/gaussian.rb

Overview

Generates a Gaussian (normally distributed) random number, using the given mean and standard deviation.

Source: stackoverflow.com/questions/5825680/code-to-generate-gaussian-normally-distributed-random-numbers-in-ruby

Instance Method Summary collapse

Constructor Details

#initialize(mean, stddev, rand_helper = lambda { Kernel.rand }) ⇒ Gaussian

Initializes the generator.

Parameters:

  • mean (Float)

    The mean.

  • stddev (Float)

    The standard deviation.

  • rand_helper (Proc) (defaults to: lambda { Kernel.rand })

    A proc used to generate the random number. This is Ruby’s rand function by default.



15
16
17
18
19
20
21
# File 'lib/random_set/gaussian.rb', line 15

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

Instance Method Details

#nextObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/random_set/gaussian.rb', line 23

def next
  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