The goal is to flip a weighted coin such that it returns heads N percent of the time.
I'm not totally stumped here. I can do it this way:
probability = 0.75 # chance of getting heads as decimal
num_heads_cases = (100.to_f * probability).to_i
cases = num_heads_cases.times.map { :heads }.concat(
(100 - num_heads_cases).times.map { :tails }
)
is_flip_result_heads = cases.sample == :heads
But I'm posting here to see if there's not some core ruby / rails method I'm overlooking. I'm looking for something similar to the following method signature:
def random_bool(probability_of_returning_true=0.5)
This is similar to the question it's marked as a duplicate of but is not a duplicate; that question asks how to get a random number, while this one asks how to get a random boolean with probability.
It might seem obvious how to go from "random number" to "random boolean" but it's evidently not that way to everyone.