Module: Random::ArrayExtensions

Defined in:
lib/garcon/core_ext/random.rb

Overview

Random extensions for Array class.

Instance Method Summary collapse

Instance Method Details

#at_randObject

Return a random element from the array.

Examples:

[1, 2, 3, 4].at_rand           # => 2
[1, 2, 3, 4].at_rand           # => 4


129
130
131
# File 'lib/garcon/core_ext/random.rb', line 129

def at_rand
  at(SecureRandom.random_number(size))
end

#at_rand!Object

Same as #at_rand, but acts in place removing a random element from the array.

Examples:

a = [1,2,3,4]
a.at_rand!       # => 2
a                # => [1,3,4]


141
142
143
# File 'lib/garcon/core_ext/random.rb', line 141

def at_rand!
  delete_at(SecureRandom.random_number(size))
end

#pick(n = nil) ⇒ Object

Similar to #at_rand, but will return an array of randomly picked exclusive elements if given a number.



147
148
149
150
151
152
153
154
# File 'lib/garcon/core_ext/random.rb', line 147

def pick(n = nil)
  if n
    a = self.dup
    a.pick!(n)
  else
    at(SecureRandom.random_number(size))
  end
end

#pick!(n = nil) ⇒ Object

Similar to #at_rand!, but given a number will return an array of exclusive elements.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/garcon/core_ext/random.rb', line 158

def pick!(n = nil)
  if n
    if n > self.size
      r = self.dup
      self.replace([])
      r
    else
      r = []
      n.times { r << delete_at(SecureRandom.random_number(size)) }
      r
    end
  else
    delete_at(SecureRandom.random_number(size))
  end
end

#rand_indexObject

Random index.



176
177
178
# File 'lib/garcon/core_ext/random.rb', line 176

def rand_index
  SecureRandom.random_number(size)
end

#rand_subarrays(n = 1) ⇒ Object

Generates random subarrays. Uses random numbers and bit-fiddling to assure performant uniform distributions even for large arrays.

Examples:

a = *1..5
a.rand_subarrays(2) # => [[3, 4, 5], []]
a.rand_subarrays(3) # => [[1], [1, 4, 5], [2, 3]]

Raises:

  • (ArgumentError)


213
214
215
216
217
218
219
# File 'lib/garcon/core_ext/random.rb', line 213

def rand_subarrays(n = 1)
  raise ArgumentError, 'negative argument' if n < 0
  (1..n).map do
    r = rand(2**self.size)
    self.select.with_index { |_, i| r[i] == 1 }
  end
end

#rand_subset(number = nil, exclusive = true) ⇒ Object

Returns a random subset of an Array. If a number of elements is specified then returns that number of elements, otherwise returns a random number of elements upto the size of the Array.

By defualt the returned values are exclusive of each other, but if exclusive is set to ‘false`, the same values can be choosen more than once.

When exclusive is true (the default) and the number given is greater than the size of the array, then all values are returned.

Examples:

[1, 2, 3, 4].rand_subset(1)        # => [2]
[1, 2, 3, 4].rand_subset(4)        # => [2, 1, 3, 4]
[1, 2, 3, 4].rand_subset           # => [1, 3, 4]
[1, 2, 3, 4].rand_subset           # => [2, 3]


197
198
199
200
201
202
203
# File 'lib/garcon/core_ext/random.rb', line 197

def rand_subset(number = nil, exclusive = true)
  number = SecureRandom.random_number(size) unless number
  number = number.to_int
  return sort_by{rand}.slice(0,number) if exclusive
  ri =[]; number.times { |n| ri << SecureRandom.random_number(size) }
  return values_at(*ri)
end

#shuffleObject

Randomize the order of an array.

Examples:

[1,2,3,4].shuffle  # => [2,4,1,3]


226
227
228
# File 'lib/garcon/core_ext/random.rb', line 226

def shuffle
  dup.shuffle!
end

#shuffle!Object

As with #shuffle but modifies the array in place. The algorithm used here is known as a Fisher-Yates shuffle.

Examples:

a = [1,2,3,4]
a.shuffle!
a  # => [2,4,1,3]


238
239
240
241
242
243
244
245
246
247
# File 'lib/garcon/core_ext/random.rb', line 238

def shuffle!
  s = size
  each_index do |j|
    i = SecureRandom.random_number(s-j)
    tmp = self[j]
    self[j] = self[j+i]
    self[j+i] = tmp
  end
  self
end