Class: Simpack::LCG

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LCG

Returns a new instance of LCG.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/simpack.rb', line 9

def initialize(options = {})
  default_options = {multiplier: 6364136223846793005,
                     modulus: 2 ** 64,
                     increment: 1442695040888963407,
                     seed: Time.now.to_i}

  options = default_options.merge(options)
  check_options(options)

  @multiplier = options[:multiplier]
  @modulus = options[:modulus]
  @increment = options[:increment]
  @seed = @x = options[:seed]
end

Instance Attribute Details

#incrementObject (readonly)

Returns the value of attribute increment.



7
8
9
# File 'lib/simpack.rb', line 7

def increment
  @increment
end

#modulusObject (readonly)

Returns the value of attribute modulus.



7
8
9
# File 'lib/simpack.rb', line 7

def modulus
  @modulus
end

#multiplierObject (readonly)

Returns the value of attribute multiplier.



7
8
9
# File 'lib/simpack.rb', line 7

def multiplier
  @multiplier
end

#seedObject (readonly)

Returns the value of attribute seed.



7
8
9
# File 'lib/simpack.rb', line 7

def seed
  @seed
end

Instance Method Details

#uniform(n = 1) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/simpack.rb', line 24

def uniform(n = 1)
  return generate_number if n == 1

  results = Array.new(n)
  (0...n).each do |i|
    results[i] = generate_number
  end
  results
end