Randomly pick from list (with duplicates)

pick  list (array), n (number_or_nil)

Pick n elements from list or ring. Unlike shuffle, after each element has been picked, it is ‘returned’ to the list so it may be picked again. This means there may be duplicates in the result. If n is greater than the size of the ring/list then duplicates are guaranteed to be in the result.

If n isn’t supplied it defaults to the size of the list/ring.

Introduced in v2.10

Options

skip:

Number of rands to skip over with each successive pick

Examples

# Example 1

puts [1, 2, 3, 4, 5].pick(3)



#=> [4, 4, 3]



# Example 2

puts (ring 1, 2, 3, 4, 5).pick(3)



#=> (ring 4, 4, 3)



# Example 3

puts (ring 1, 2).pick(5)



#=> (ring 2, 2, 1, 1, 1)



# Example 4

puts (ring 1, 2, 3).pick



#=> (ring 3, 3, 2)