I'm currently making a little script. Whenever the user hits submit in this script, he/she should have X% chance of winning. Currently I have this:
function winningChance() {
$won = "you lost";
$ret = 2 * rand(1, 50);
if($ret == 100) {
$won = "you won!";
}
return $won;
}
echo winningChance();
Although this is not very advanced, how can I do so I can set the percentage chance of winning?
function winningChance(int $percentage): string
{
if ($percentage < 0 || $percentage > 100) {
throw new \Exception('Invalid percentage');
}
return rand(1, 100) <= $percentage ? 'won' : 'lost';
}
echo "You've " . winningChance(50) . '!';