module SampleByRate

Constants

VERSION

Public Instance Methods

sample_by_rate(rate) { |n| ... } click to toggle source
# File lib/sample_by_rate.rb, line 4
def sample_by_rate(rate)
  raise 'rate must greater than or equal to 0 and less than  or equal to 1' unless rate >= 0 and rate <= 1

  return enum_for(__method__, rate) unless block_given?

  each {|n| yield n if rand < rate }
end