Sha256: 20fb38f152de5347dc24efb5495ead719209bd4be7a8b78fc7f4454892258844
Contents?: true
Size: 1 KB
Versions: 9
Compression:
Stored size: 1 KB
Contents
class Array # Return random number indexes of an Array. If the _number_ # of elements is set to <tt>nil</tt>, the default, then # returns a random number of indicies up to the size of the # array. # # When _exclusive_ is <tt>true</tt> (the default) and the # _number_ given is greater than the size of the array, # then all indexes are returned. # # ["a", "b", "c", "d"].rand_indexes #=> [1] # ["a", "b", "c", "d"].rand_indexes #=> [2,4] # ["a", "b", "c", "d"].rand_indexes(2) #=> [2,1] # ["a", "b", "c", "d"].rand_indexes(3) #=> [1,3,2] # ["a", "b", "c", "d"].rand_indexes(3,false) #=> [4,3,4] # def rand_indexes( number=nil, exclusive=true ) n = rand( size ) unless number n = number.to_int if exclusive idx = (0...size).to_a return idx if number >= size return idx.sort_by{rand}.slice(0,n) else a = [] n.times{ a << rand(size) } a end end alias_method :rand_indicies, :rand_indexes end
Version data entries
9 entries across 9 versions & 1 rubygems