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.
256 257 258 |
# File 'lib/ffaker.rb', line 256 def self.new_rng ::Random.new(seed) end |
.rand(max = nil) ⇒ Object
Returns a random number using an RNG with a known seed.
244 245 246 247 248 |
# File 'lib/ffaker.rb', line 244 def self.rand(max = nil) return rng.rand(max) if max rng.rand end |
.reset! ⇒ Object
Reset the RNG back to its initial state.
239 240 241 |
# File 'lib/ffaker.rb', line 239 def self.reset! @rng = new_rng end |
.rng ⇒ Object
Returns the current Random object.
251 252 253 |
# File 'lib/ffaker.rb', line 251 def self.rng @rng ||= new_rng end |
.seed ⇒ Object
Returns the current RNG seed.
227 228 229 |
# File 'lib/ffaker.rb', line 227 def self.seed @seed ||= ::Random.new_seed end |
.seed=(new_seed) ⇒ Object
Sets the RNG seed and creates a new internal RNG.
232 233 234 235 236 |
# File 'lib/ffaker.rb', line 232 def self.seed=(new_seed) @seed = new_seed reset! new_seed end |