mardi 30 octobre 2018

Trying to get a random number with a chance to get that number

I tried to create this, but it does not really, work. Here is the table of how I want it to work.. click to see picture

But it doesnt work like that. Here is a picture of my database (in phpmyadmin) click to see picture

I want it to first look at the percentages, and find which one to use. So 75% chance to get a random number between 1 and 50. 20% chance for a random number between 51 and 200 etc.


Here is my code;

if ($_GET['method'] == "test") {
    $sql = "SELECT 1percentage, 1min, 1max, 2percentage, 2min, 2max, 3percentage, 3min, 3max, 4percentage, 4min, 4max FROM `keys`";
    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()) {
            $total = $row["1percentage"] + $row["2percentage"] + $row["3percentage"] + $row["4percentage"] + 1;
            $random = rand(0, $total);
            if ($random < $row['1percentage'] || $random == $row['1percentage']) {
                $amount = rand($row['1min'], $row['1max']);
            } elseif ($random > $row['1percentage'] && $random < $row['2percentage'] || $random == $row['2percentage']) {
                $amount = rand($row['2min'], $row['2max']);
            } elseif ($random > $row['2percentage'] && $random < $row['3percentage'] || $random == $row['3percentage']) {
                $amount = rand($row['3min'], $row['3max']);
            } elseif ($random > $row['4percentage'] || $random == $row['4percentage']) {
                $amount = rand($row['4min'], $row['4max']);
            } else {
                exit("0");
            }

            echo $amount;
        }
}

But what it outputs is or 1 to 50, or 1001 to 10000. So what did I do wrong? Thanks!




Aucun commentaire:

Enregistrer un commentaire