What are the best practices for writing PHP code to determine the number of dice rolls needed to achieve a specific outcome?

When determining the number of dice rolls needed to achieve a specific outcome, one approach is to simulate the dice rolls in PHP code and keep track of the number of rolls until the desired outcome is met. This can be achieved by using a loop that generates random dice rolls and checks if the desired outcome is achieved. Once the outcome is met, the loop can be exited and the number of rolls can be returned.

function getNumberOfRollsToAchieveOutcome($outcome) {
    $rolls = 0;
    
    do {
        $roll = rand(1, 6); // simulate a dice roll
        $rolls++;
    } while ($roll != $outcome);
    
    return $rolls;
}

$outcome = 6; // specify the desired outcome
$rollsNeeded = getNumberOfRollsToAchieveOutcome($outcome);
echo "Number of rolls needed to achieve outcome $outcome: $rollsNeeded";