Module: FFaker::RandomUtils

Included in:
ArrayUtils, ModuleUtils
Defined in:
lib/ffaker/utils/random_utils.rb

Overview

Methods for returning repeatably-random data using the internal Random Number Generator. You should not need to use this directly, it is automatically included when you `include ModuleUtils` in a FFaker module.

Instance Method Summary collapse

Instance Method Details

#fetch_sample(list, options = {}) ⇒ Object

Performs Array#sample on `list` using a the internal Random Number Generator so that the results are deterministic.

  • Returns one random item from `list`.

  • Pass `count: n` in options argument, where `n` is an integer, to

return n items from `list`


20
21
22
23
24
25
26
27
28
# File 'lib/ffaker/utils/random_utils.rb', line 20

def fetch_sample(list, options = {})
  if (count = options.delete(:count))
    list.sample(count, random: FFaker::Random)
  elsif list.is_a?(Range)
    FFaker::Random.rand(list)
  else
    list.sample(random: FFaker::Random)
  end
end

#rand(max = nil) ⇒ Object

Returns a randon number from the internal Random Number Generator. Can be used in place of `rand` or `Kernal.rand`.


10
11
12
# File 'lib/ffaker/utils/random_utils.rb', line 10

def rand(max = nil)
  FFaker::Random.rand(max)
end

#shuffle(list) ⇒ Object

Performs same action as as `Array#suffle` (returns a randomly-reordered copy of `list`) except that it uses the internal Random Number Generator so that the results are deterministic.


33
34
35
# File 'lib/ffaker/utils/random_utils.rb', line 33

def shuffle(list)
  list.shuffle(random: FFaker::Random)
end