Module: KeePass::Random

Defined in:
lib/keepass/random.rb

Class Method Summary collapse

Class Method Details

.random_number(n = 0) ⇒ Integer|Float

If n is a positive integer, then returns a random integer r such that 0 <= r < n.

If n is 0 or unspecified, then returns a random float r such that 0 <= r < 1.

Parameters:

  • n (Integer) (defaults to: 0)

    the upper bound

Returns:

  • (Integer|Float)

    the random number

See Also:

  • ActiveSupport::SecureRandom#random_number


16
17
18
# File 'lib/keepass/random.rb', line 16

def self.random_number(n = 0)
  SecureRandom.random_number(n)
end

.sample_array(array) ⇒ Object

Returns a randomly sampled item from the array.

Parameters:

  • array (Array)

    the array to sample from

Returns:

  • (Object)

    random item or nil if no items exist



24
25
26
# File 'lib/keepass/random.rb', line 24

def self.sample_array(array)
  array[random_number(array.size)]
end

.shuffle_array(array) ⇒ Array

Returns the array shuffled randomly.

Parameters:

  • array (Array)

    the array to shuffle

Returns:

  • (Array)

    the shuffled array



32
33
34
# File 'lib/keepass/random.rb', line 32

def self.shuffle_array(array)
  array.sort_by { random_number }
end