Random Number Php Produced

My task:

Generate random numbers from 1 to 20, up to 1 decimal place.

However, my problem is as simple as mt_rand. I want most of the numbers to be lower than 0.5 - 4.5, with the number of episodes between 4.5-10 and very rarely repeated every 12-20 hours between 10-20.

I used the following, but have no idea where to go. I am a very simple self-taught programmer.

$min = 1;
$max = 20;
$suisse_interest = mt_rand ($min*10, $max*10) / 10

Perhaps if I briefly explain why I want this, this may help.

I own an online game and I want to add 3 “banks” with each bank creating different interests every hour. Most of the time I want it to be low, but sometimes high and very rarely very high (15-20%).

With the code above, the random number often gets too high.

Any help with this is much appreciated!

+4
3

. 1,0 ~ 4,5 96%, 4,5 ~ 10,0 - 2%, 10,0 ~ 20,0 - 2%.

<?php
    // 1.0~4.5    96%
    // 4.5~10.0   2%
    // 10.0~20.0  2%

    function fun() {
        $num = mt_rand(1, 100);
        if ($num > 0 && $num <= 96) {
            $return = mt_rand(10, 45) / 10;  // 96%
        } else if ($num > 96 && $num <= 98) {
            $return = mt_rand(45, 100) / 10;  // 2%
        } else {
            $return = mt_rand(100, 200) / 10;  // 2%
        }
        return sprintf("%01.1f",$return);
    }

    echo fun();
?>
+2

. , , . , , , .

$i = 0;
while($i<30) {
    $i++;
    $rand = mt_rand(0, 7000) / 100; // 0.0 ~ 70.0
    // This is the most important line:
    $output = round( 20*(pow(0.95,$rand)) , 1);
    echo "$output ";
}

:

1.8  4.3  2.6  5.5  3.7  15.5  1.6  0.6  0.6  1.6  5.8  
1.3  6.1  3.2  0.8  1.7  14.7  7.9  1.3  10.3  5.5  12.6  
1.5  8.4  1.5  0.9  13.3  5.8  7.5  1.7  

, .

Chart

20 1.4% , smaller than 5 78%

+3

, PHP.

, , PHP.

, - , aka normal distribution, , .

, .

3 4 , .

By summing up the random values ​​between 0 and twice with your average course, you will create a Gaussian approximation centered around your average value. Then you can pin values ​​below the midpoint if you don't want low rates. The end result will be a 50% chance of getting an average course and a steadily decreasing chance of getting up to twice that.

An increasing number of sums will “narrow” the curve, making it less likely to get high value.

eg:

define ("INTEREST_MEAN", 10);
define ("INTEREST_SPREAD", 5);
function random_interest ()
{
    $res = 0;
    for ($i = 0 ; $i != INTEREST_SPREAD ; $i++) $res += mt_rand(0, 2*INTEREST_MEAN);
    $res /= INTEREST_SPREAD; // normalize the sum to get a mean-centered result
    $res = max ($res, INTEREST_MEAN); // clamp lower values
}
0
source

All Articles