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.


239
240
241
# File 'lib/ffaker.rb', line 239

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

.rand(max = nil) ⇒ Object

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


227
228
229
230
231
# File 'lib/ffaker.rb', line 227

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

  rng.rand
end

.reset!Object

Reset the RNG back to its initial state.


222
223
224
# File 'lib/ffaker.rb', line 222

def self.reset!
  @rng = new_rng
end

.rngObject

Returns the current Random object.


234
235
236
# File 'lib/ffaker.rb', line 234

def self.rng
  @rng ||= new_rng
end

.seedObject

Returns the current RNG seed.


210
211
212
# File 'lib/ffaker.rb', line 210

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

.seed=(new_seed) ⇒ Object

Sets the RNG seed and creates a new internal RNG.


215
216
217
218
219
# File 'lib/ffaker.rb', line 215

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