One way to generate a random boolean in Ruby is to use the rand
method to return a random float between 0 and 1, and then compare it to a threshold value.
main.rb100 chars5 lines
This function takes an optional probability p
(defaulting to 0.5), and returns true
with probability p
, and false
with probability 1 - p
.
Here's an example usage:
main.rb108 chars5 lines
This will output something like:
main.rb52 chars11 lines
gistlibby LogSnag