Set up a random force value generation algorithm

A few days ago, you helped me find an algorithm for generating random force values ​​in an online game (especially John Rush) .

function getRandomStrength($quality) {
    $rand = mt_rand()/mt_getrandmax();
    $value = round(pow(M_E, ($rand - 1.033) / -0.45), 1);
    return $value;
}

This function generates values ​​from 1.1 to 9.9. Now I want to configure this function so that it gives me values ​​with the same probability, but in a different interval, for example. 1.5-8.0. It would be ideal if you could achieve this with additional parameters.

It would be great if you could help me. Thanks in advance!

+3
source share
3 answers

1.033 -0.45 , 1.1-9.9. , 1.1 9.9 $low $high .

function getRandomStrength($low, $high) {
    // TODO: validate the input
    $ln_low = log( $low, M_E );
    $ln_high = log( $high, M_E );
    $scale = $ln_high - $ln_low;

    $rand = ( mt_rand() / mt_getrandmax() ) * $scale + $ln_low;
    $value = round( pow( M_E, $rand), 1 );
    return $value;
}

$low $high . ( , 0 < $low < $high .)

, . , 1,1 - 9,9, , , 0,093 - 2,2925. e , .

+5

- :

function getRandomStrength($quality,$min,$max) {
    $rand = mt_rand()/mt_getrandmax();
    $value = round(pow(M_E, ($rand - 1.033) / -0.45), 1);
    $value = $value - 1.1
    $value = $value * ((max-min) / 8.8)
    $value = $value + $min
    return $value;
}
+3

:

D(a,b) = (D(0,1)*(b-a))+a

D (0,1) D (c, d), :

D(0,1) = (D(c,d)-c)/(d-c)

D ( ), a 1,5, b 8.5, c 1.1, d - 9.9

+2

Source: https://habr.com/ru/post/1709698/


All Articles