Files
cs-252/lab19/with-prob.rb
2026-04-29 11:33:23 -07:00

21 lines
265 B
Ruby

def with_prob (prob)
yield if (Random.rand < prob)
end
with_prob 0.42 do
puts "There is a 42% chance that this code will print"
end
def foo x
with_prob 0.5 do
puts "Executing with_prob block"
return 0
end
return x
end
puts "Foo is #{foo 1}"