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
deprecated
Deprecated.
Use Array#sample instead.
-
#at_rand! ⇒ Object
deprecated
Deprecated.
Use Array#sample! instead.
-
#pick(n = nil) ⇒ Object
deprecated
Deprecated.
Use Array#sample instead.
-
#pick!(n = nil) ⇒ Object
deprecated
Deprecated.
Use Array#sample! instead.
-
#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.
-
#sample!(n = nil) ⇒ Object
Destructive version of Array#sample.
Instance Method Details
#at_rand ⇒ Object
Use Array#sample instead.
120 121 122 123 |
# File 'lib/standard/facets/random.rb', line 120 def at_rand warn "Array#at_rand is deprecated. Use Array#sample instead.", uplevel: 1 sample end |
#at_rand! ⇒ Object
Use Array#sample! instead.
126 127 128 129 |
# File 'lib/standard/facets/random.rb', line 126 def at_rand! warn "Array#at_rand! is deprecated. Use Array#sample! instead.", uplevel: 1 sample! end |
#pick(n = nil) ⇒ Object
Use Array#sample instead.
132 133 134 135 |
# File 'lib/standard/facets/random.rb', line 132 def pick(n=nil) warn "Array#pick is deprecated. Use Array#sample instead.", uplevel: 1 n ? sample(n) : sample end |
#pick!(n = nil) ⇒ Object
Use Array#sample! instead.
138 139 140 141 |
# File 'lib/standard/facets/random.rb', line 138 def pick!(n=nil) warn "Array#pick! is deprecated. Use Array#sample! instead.", uplevel: 1 sample!(n) end |
#rand_index ⇒ Object
Random index.
145 146 147 |
# File 'lib/standard/facets/random.rb', line 145 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
176 177 178 179 180 181 182 |
# File 'lib/standard/facets/random.rb', line 176 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.
[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]
159 160 161 162 163 164 165 |
# File 'lib/standard/facets/random.rb', line 159 def rand_subset(number=nil, exclusive=true) number = Random.number(size) unless number number = number.to_int return sort_by{rand}.slice(0,number) if exclusive ri =[]; number.times { |n| ri << Random.number(size) } return values_at(*ri) end |
#sample!(n = nil) ⇒ Object
Destructive version of Array#sample. Removes and returns a random element, or n random exclusive elements.
a = [1,2,3,4]
a.sample! #~> 2
a #~> [1,3,4]
a = [1,2,3,4,5]
a.sample!(3) #~> [2,4,1]
a #~> [3,5]
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/standard/facets/random.rb', line 103 def sample!(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 |