Answer the question
In order to leave comments, you need to log in
How to correctly express the probability of success in code?
Is there a general approach to turning a percentage of a probability into the result of an action in programming?
Let's say I bought a lottery ticket with a 50% chance of winning. How do I turn this into a function that gives yes or no based on that percentage?
Would it be correct to convert percentages to fractions, and if the numerator falls on the randomizer, then the function returns - True?
Answer the question
In order to leave comments, you need to log in
function getSuccesByСhance($chance)
{
return mt_rand(0, 100) <= $chance;
};
var_dump(getSuccesByСhance(30)); //false
var_dump(getSuccesByСhance(30)); //true
var_dump(getSuccesByСhance(30)); //true
var_dump(getSuccesByСhance(30)); //false
var_dump(getSuccesByСhance(30)); //true
var_dump(getSuccesByСhance(30)); //false
var_dump(getSuccesByСhance(30)); //false
var_dump(getSuccesByСhance(30)); //false
var_dump(getSuccesByСhance(30)); //false
var_dump(getSuccesByСhance(30)); //false
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question