Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/facets/random.rb

Instance Method Summary collapse

Instance Method Details

#at_randObject

Return a random element of the array.

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


40
41
42
# File 'lib/facets/random.rb', line 40

def at_rand
  self.at( rand( 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]


51
52
53
# File 'lib/facets/random.rb', line 51

def at_rand!
  return delete_at( Kernel.rand( size ) )
end

#pick(n = nil) ⇒ Object

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



57
58
59
60
61
62
63
64
# File 'lib/facets/random.rb', line 57

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

#pick!(n = nil) ⇒ Object

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/facets/random.rb', line 68

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

#rand_indexObject

Random index.



86
87
88
# File 'lib/facets/random.rb', line 86

def rand_index
  rand( size )
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]


108
109
110
111
112
113
114
115
# File 'lib/facets/random.rb', line 108

def rand_subset( number=nil, exclusive=true )
  number = rand( 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 << rand( size ) }
  return values_at(*ri)
end

#shuffleObject

Randomize the order of an array.

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


121
122
123
124
# File 'lib/facets/random.rb', line 121

def shuffle
  dup.shuffle!
  #sort_by{Kernel.rand}
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]


135
136
137
138
139
140
141
142
143
144
145
# File 'lib/facets/random.rb', line 135

def shuffle!
  s = size
  each_index do |j|
    i = Kernel.rand(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