Module: KeePass::Random
- Defined in:
- lib/keepass/random.rb
Class Method Summary collapse
-
.random_number(n = 0) ⇒ Integer|Float
If
nis a positive integer, then returns a random integerrsuch that 0 <=r<n. -
.sample_array(array) ⇒ Object
Returns a randomly sampled item from the array.
-
.shuffle_array(array) ⇒ Array
Returns the array shuffled randomly.
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.
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.
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.
32 33 34 |
# File 'lib/keepass/random.rb', line 32 def self.shuffle_array(array) array.sort_by { random_number } end |