Module: FFaker::Random

Defined in:
lib/ffaker.rb

Overview

Random Number Generator (RNG) used with ModuleUtils#fetch, #shuffle, #rand in order to provide deterministic repeatability.

Class Method Summary collapse

Class Method Details

.new_rngObject

Returns a new Random object instantiated with #seed.



125
126
127
# File 'lib/ffaker.rb', line 125

def self.new_rng
  ::Random.new(seed)
end

.rand(max = nil) ⇒ Object

Returns a random number using an RNG with a known seed.



113
114
115
116
117
# File 'lib/ffaker.rb', line 113

def self.rand(max = nil)
  return rng.rand(max) if max

  rng.rand
end

.reset!Object

Reset the RNG back to its initial state.



108
109
110
# File 'lib/ffaker.rb', line 108

def self.reset!
  @rng = new_rng
end

.rngObject

Returns the current Random object.



120
121
122
# File 'lib/ffaker.rb', line 120

def self.rng
  @rng ||= new_rng
end

.seedObject

Returns the current RNG seed.



96
97
98
# File 'lib/ffaker.rb', line 96

def self.seed
  @seed ||= ::Random.new_seed
end

.seed=(new_seed) ⇒ Object

Sets the RNG seed and creates a new internal RNG.



101
102
103
104
105
# File 'lib/ffaker.rb', line 101

def self.seed=(new_seed)
  @seed = new_seed
  reset!
  new_seed
end