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.


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

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

.rand(max = nil) ⇒ Object

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


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

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

  rng.rand
end

.reset!Object

Reset the RNG back to its initial state.


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

def self.reset!
  @rng = new_rng
end

.rngObject

Returns the current Random object.


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

def self.rng
  @rng ||= new_rng
end

.seedObject

Returns the current RNG seed.


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

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

.seed=(new_seed) ⇒ Object

Sets the RNG seed and creates a new internal RNG.


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

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