Module: Random::ArrayExtensions
- Included in:
- Array
- Defined in:
- lib/standard/facets/random.rb
Overview
Random extensions for Array class.
Instance Method Summary collapse
-
#at_rand ⇒ Object
Return a random element from the array.
-
#at_rand! ⇒ Object
Same as #at_rand, but acts in place removing a random element from the array.
-
#pick(n = nil) ⇒ Object
Similar to #at_rand, but will return an array of randomly picked exclusive elements if given a number.
-
#pick!(n = nil) ⇒ Object
Similar to #at_rand!, but given a number will return an array of exclusive elements.
-
#rand_index ⇒ Object
Random index.
-
#rand_subarrays(n = 1) ⇒ Object
Generates random subarrays.
-
#rand_subset(number = nil, exclusive = true) ⇒ Object
Returns a random subset of an Array.
-
#shuffle ⇒ Object
Randomize the order of an array.
-
#shuffle! ⇒ Object
As with #shuffle but modifies the array in place.
Instance Method Details
#at_rand ⇒ Object
Return a random element from the array.
[1, 2, 3, 4].at_rand #~> 2
[1, 2, 3, 4].at_rand #~> 4
141 142 143 |
# File 'lib/standard/facets/random.rb', line 141 def at_rand at(Random.number(size)) end |
#at_rand! ⇒ Object
Same as #at_rand, but acts in place removing a random element from the array.
a = [1,2,3,4]
a.at_rand! #~> 2
a #~> [1,3,4]
152 153 154 |
# File 'lib/standard/facets/random.rb', line 152 def at_rand! return delete_at( 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.
158 159 160 161 162 163 164 165 |
# File 'lib/standard/facets/random.rb', line 158 def pick(n=nil) if n a = self.dup a.pick!(n) else at(Random.number(size)) end end |
#pick!(n = nil) ⇒ Object
Similar to #at_rand!, but given a number will return an array of exclusive elements.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/standard/facets/random.rb', line 169 def pick!(n=nil) if n if n > self.size r = self.dup self.replace([]) r else r = [] n.times { r << delete_at(Random.number(size)) } r end else delete_at(Random.number(size)) end end |
#rand_index ⇒ Object
Random index.
187 188 189 |
# File 'lib/standard/facets/random.rb', line 187 def rand_index 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.
a = *1..5
a.(2) #=> [[3, 4, 5], []]
a.(3) #=> [[1], [1, 4, 5], [2, 3]]
CREDIT: Michael Kohl
227 228 229 230 231 232 233 |
# File 'lib/standard/facets/random.rb', line 227 def (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.
[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]
209 210 211 212 213 214 215 216 |
# File 'lib/standard/facets/random.rb', line 209 def rand_subset(number=nil, exclusive=true) number = Random.number(size) unless number number = number.to_int #return self.dup if (number >= size and exlusive) return sort_by{rand}.slice(0,number) if exclusive ri =[]; number.times { |n| ri << Random.number(size) } return values_at(*ri) end |
#shuffle ⇒ Object
Randomize the order of an array.
[1,2,3,4].shuffle #~> [2,4,1,3]
239 240 241 242 |
# File 'lib/standard/facets/random.rb', line 239 def shuffle dup.shuffle! #sort_by{Random.number} end |
#shuffle! ⇒ Object
As with #shuffle but modifies the array in place. The algorithm used here is known as a Fisher-Yates shuffle.
a = [1,2,3,4]
a.shuffle!
a #~> [2,4,1,3]
CREDIT Niel Spring
253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/standard/facets/random.rb', line 253 def shuffle! s = size each_index do |j| i = Random.number(s-j) #self[j], self[j+i] = self[j+i], self[j] tmp = self[j] self[j] = self[j+i] self[j+i] = tmp end self end |