2

Following this simple but very convenient method to sample from an array without replacement, we can see something like

arr = ["a", "b", "c", "d", "e", "f"]
arr.sample(10)
=> ["b", "a", "d", "f", "e", "c"]

How is same accomplished, but with replacement? So in the example .sample(10) would return an array of length 10 with some duplicate elements?

e.g.

arr = ["a", "b", "c", "d", "e", "f"]
arr.sample(10)
=> ["b", "a", "d", "f", "e", "c", "b", "e", "a", "b"]
stevec
  • 41,291
  • 27
  • 223
  • 311

1 Answers1

3

take 1 sample 10 times and collect the result:

 10.times.map{arr.sample}
steenslag
  • 79,051
  • 16
  • 138
  • 171