PHP的钟形曲线算法

Jav*_*vit 1 php algorithm

我正在开展一个个人项目,其中IQ范围将随机分配给假人物.这种对齐将是随机的,但却是现实的,因此IQ范围必须沿着钟形曲线分布.有3个范围类别:低,正常和高.假字符的一半将落在正常范围内,但约25%将落入低或高范围.

我该如何编码呢?

Mar*_*ker 5

它可能看起来冗长而复杂(并且是为PHP4编写的程序)但我曾经使用以下方法生成非线性随机分布:

function random_0_1()
{
    //  returns random number using mt_rand() with a flat distribution from 0 to 1 inclusive
    //
    return (float) mt_rand() / (float) mt_getrandmax() ;
}

function random_PN()
{
    //  returns random number using mt_rand() with a flat distribution from -1 to 1 inclusive
    //
    return (2.0 * random_0_1()) - 1.0 ;
}


function gauss()
{
    static $useExists = false ;
    static $useValue ;

    if ($useExists) {
        //  Use value from a previous call to this function
        //
        $useExists = false ;
        return $useValue ;
    } else {
        //  Polar form of the Box-Muller transformation
        //
        $w = 2.0 ;
        while (($w >= 1.0) || ($w == 0.0)) {
            $x = random_PN() ;
            $y = random_PN() ;
            $w = ($x * $x) + ($y * $y) ;
        }
        $w = sqrt((-2.0 * log($w)) / $w) ;

        //  Set value for next call to this function
        //
        $useValue = $y * $w ;
        $useExists = true ;

        return $x * $w ;
    }
}

function gauss_ms( $mean,
                   $stddev )
{
    //  Adjust our gaussian random to fit the mean and standard deviation
    //  The division by 4 is an arbitrary value to help fit the distribution
    //      within our required range, and gives a best fit for $stddev = 1.0
    //
    return gauss() * ($stddev/4) + $mean;
}

function gaussianWeightedRnd( $LowValue,
                                 $maxRand,
                                 $mean=0.0,
                                 $stddev=2.0 )
{
    //  Adjust a gaussian random value to fit within our specified range
    //      by 'trimming' the extreme values as the distribution curve
    //      approaches +/- infinity
    $rand_val = $LowValue + $maxRand ;
    while (($rand_val < $LowValue) || ($rand_val >= ($LowValue + $maxRand))) {
        $rand_val = floor(gauss_ms($mean,$stddev) * $maxRand) + $LowValue ;
        $rand_val = ($rand_val + $maxRand) / 2 ;
    }

    return $rand_val ;
}

function bellWeightedRnd( $LowValue,
                             $maxRand )
{
    return gaussianWeightedRnd( $LowValue, $maxRand, 0.0, 1.0 ) ;
}
Run Code Online (Sandbox Code Playgroud)

对于简单的贝尔分布,只需使用最小值和最大值调用bellWeightedRnd(); 对于更复杂的分布,gaussianWeightedRnd()允许您指定分布的均值和stdev.

高斯钟形曲线非常适合IQ分布,尽管我也有类似的例程用于替代分布曲线,例如泊松,伽马,对数和c.