PHP的钟形曲线算法

时间:2011-03-04 02:06:08

标签: php algorithm

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

我该如何编码?

4 个答案:

答案 0 :(得分: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 ) ;
}

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

高斯钟形曲线非常适合IQ分布,尽管我也有类似的例子用于替代分布曲线,例如泊松,伽玛,对数和&lt; c。

答案 1 :(得分:1)

首先假设您有3个功能来提供高中低智商,然后只需

function randomIQ(){
    $dice = rand(1,100);
    if($dice <= 25) $iq = low_iq();
    elseif($dice <= 75) $iq = medium_iq();
    else $iq = high_iq();
    return $iq;
}

答案 2 :(得分:0)

你可以随机化多个'骰子',随机数从每个加起来到最高点。这将产生正态分布(大约)。

答案 3 :(得分:0)

使用发布的link我创建了以下函数:

function RandomIQ()
{  
    return round((rand(-1000,1000) + rand(-1000,1000) + rand(-1000,1000))/100,0) * 2 + 100;
}   

它有点乱,但是一些快速检查给它一个大约100的平均值和一个大致正态分布。它应该与我从site获得的信息一致。

相关问题