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 a size of 1.

If no arguments are given, will return a lambda function which when called takes an argument which will be a list to be picked from. This is useful for choosing random onset: vals for samples.

Always returns a list-like thing (either an array or ring)

Introduced in v2.10

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)



# Example 5


live_loop :foo do
  sample :loop_amen, onset: pick  
  sleep 0.125
end



# Using pick for random sample onsets
 
# pick a random onset value each time