Module | Random::Array |
In: |
lib/more/facets/random.rb
|
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]
Similar to at_rand, but will return an array of randomly picked exclusive elements if given a number.
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]