PHP
<?php
$preise = array("Teddy" => 0.10, "Auto" => 0.05);
foreach ( $preise as $preis => $chance ) {
if( ($rolled = random(0, 100)) <= ($chance * 100) ) {
$won = $preis;
break;
}
}
if( isset($won) ) {
echo "You won $won !!!11einself1 ;; you rolled $rolled , the chance was " . ($preise[$won]*100) . "% ! Congrats";
}
?>
Alles anzeigen
sowas?
//P.S. Ist natürlich so beschiss Wenn du's ehrlich machen willst:
PHP
<?php
$preise = array("Auto" => 0.05, "Teddy" => 0.10); // ordered by chance ASC
$rolled = random(0, 100);
foreach ( $preise as $preis => $chance ) {
if( ($rolled <= ($chance * 100) ) { // $chance could be already mutiplied in SQL or actually saved in percent.. Who cares.
$won = $preis;
break;
}
}
if( isset($won) ) {
echo "You won $won !!!11einself1 ;; you rolled $rolled , the chance was " . ($preise[$won]*100) . "% ! Congrats";
}
?>
Alles anzeigen
oder aber auch gleich mit SQL... (indem fall ausgehend von % angaben als int in der DB)
Zitat
SELECT (RAND() * 100) as rolled, chance, name FROM articles WHERE chance > rolled ORDER BY chance LIMIT 1