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
-
.new_rng ⇒ Object
Returns a new Random object instantiated with #seed.
-
.rand(max = nil) ⇒ Object
Returns a random number using an RNG with a known seed.
-
.reset! ⇒ Object
Reset the RNG back to its initial state.
-
.rng ⇒ Object
Returns the current Random object.
-
.seed ⇒ Object
Returns the current RNG seed.
-
.seed=(new_seed) ⇒ Object
Sets the RNG seed and creates a new internal RNG.
Class Method Details
.new_rng ⇒ Object
Returns a new Random object instantiated with #seed.
71 72 73 |
# File 'lib/ffaker.rb', line 71 def self.new_rng ::Random.new(seed) end |
.rand(max = nil) ⇒ Object
Returns a random number using an RNG with a known seed.
57 58 59 60 61 62 63 |
# File 'lib/ffaker.rb', line 57 def self.rand(max = nil) if max rng.rand(max) else rng.rand end end |
.reset! ⇒ Object
Reset the RNG back to its initial state.
52 53 54 |
# File 'lib/ffaker.rb', line 52 def self.reset! @rng = new_rng end |
.rng ⇒ Object
Returns the current Random object.
66 67 68 |
# File 'lib/ffaker.rb', line 66 def self.rng @rng ||= new_rng end |
.seed ⇒ Object
Returns the current RNG seed.
40 41 42 |
# File 'lib/ffaker.rb', line 40 def self.seed @random_seed ||= ::Random.new_seed end |
.seed=(new_seed) ⇒ Object
Sets the RNG seed and creates a new internal RNG.
45 46 47 48 49 |
# File 'lib/ffaker.rb', line 45 def self.seed=(new_seed) @random_seed = new_seed reset! new_seed end |