To get a sample of 5 elements from an array with replacement in Ruby, you can use the sample
method with an argument of the desired sample size, and rand()
to generate the index of the sampled element:
main.rb120 chars6 lines
This code will output 5 randomly sampled elements from the original array, with replacement. The Array.new
method creates a new array of size 5 and fills it with elements sampled from the original array using rand()
to generate a random index.
gistlibby LogSnag