Odds Based Random Number Generator
Join the DZone community and get the full member experience.Join For Free
Worthy of a snippet in its own right as well as a smaller part of this snippet. Full detail on its use is here, but it is a way of returning a random true or false based on the odds.
class Odds def self.true?(chance=0) (rand(chance)<1) end endFor example for 1 in 4 odds you would call
Odds.true?(4)For a dead cert true? or true?(0) would always return true. Check out the other links for a slightly modified version which is optimised for known low failure rates.
Opinions expressed by DZone contributors are their own.