In: |
rand.rb
|
Parent: | Object |
Choose and return a random element of self.
[1, 2, 3, 4].pick #=> 2 (or 1, 3, 4)
# File rand.rb, line 40 40: def pick 41: self[pick_index] 42: end
Deletes a random element of self, returning that element.
a = [1, 2, 3, 4] a.pick #=> 2 a #=> [1, 3, 4]
# File rand.rb, line 48 48: def pick! 49: i = pick_index 50: rv = self[i] 51: delete_at(i) 52: rv 53: end
Return the index of an random element of self.
["foo", "bar", "baz"].pick_index #=> 1 (or 0, or 2)
# File rand.rb, line 57 57: def pick_index 58: rand(size) 59: end
Destructive pick_index. Delete a random element of self and return its index.
a = [11, 22, 33, 44] a.pick_index! #=> 2 a #=> [11, 22, 44]
# File rand.rb, line 66 66: def pick_index! 67: i = pick_index 68: delete_at i 69: i 70: end
Return an array of the elements in random order.
[11, 22, 33, 44].shuffle #=> [33, 11, 44, 22]
# File rand.rb, line 74 74: def shuffle 75: sort_by{rand} 76: end