Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于PHP的Bell曲线算法_Php_Algorithm - Fatal编程技术网

基于PHP的Bell曲线算法

基于PHP的Bell曲线算法,php,algorithm,Php,Algorithm,我正在做一个个人项目,其中IQ范围将随机分配给假角色。这个假设是随机的,但现实的,所以IQ范围必须沿着钟形曲线分布。有3个范围类别:低、正常和高。一半的假字符属于正常范围,但大约25%的假字符属于低或高范围 我该如何编码呢?您可以将多个“骰子”随机排列,每个骰子上的随机数相加到最高点。这将产生一个正态分布(近似)。您可以随机选择多个“骰子”,每个骰子的随机数相加到最高点。这将生成一个正态分布(大约)。使用我创建的以下函数: function RandomIQ() { return r

我正在做一个个人项目,其中IQ范围将随机分配给假角色。这个假设是随机的,但现实的,所以IQ范围必须沿着钟形曲线分布。有3个范围类别:低、正常和高。一半的假字符属于正常范围,但大约25%的假字符属于低或高范围


我该如何编码呢?

您可以将多个“骰子”随机排列,每个骰子上的随机数相加到最高点。这将产生一个正态分布(近似)。

您可以随机选择多个“骰子”,每个骰子的随机数相加到最高点。这将生成一个正态分布(大约)。

使用我创建的以下函数:

function RandomIQ()
{  
    return round((rand(-1000,1000) + rand(-1000,1000) + rand(-1000,1000))/100,0) * 2 + 100;
}   
这有点混乱,但快速检查后,平均值约为100,大致为正态分布。它应该与我从中获得的信息一致。

使用ithcy发布的信息,我创建了以下函数:

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

这有点混乱,但快速检查后,平均值约为100,大致为正态分布。它应该符合我从中得到的信息。

首先假设您有3个函数来提供高、中、低IQ,然后

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

如果($dice首先假设您有3个函数来提供高、中、低IQ,那么

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

如果($dice它可能看起来又长又复杂(并且是为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 ) ;
}
函数随机_0_1()
{
//使用mt_rand()返回随机数,平均分布范围为0到1(含0到1)
//
返回(浮动)mt_rand()/(浮动)mt_getrandmax();
}
函数随机_PN()
{
//使用mt_rand()返回随机数,平均分布范围为-1到1(含1)
//
返回值(2.0*random_0_1())-1.0;
}
函数gauss()
{
静态$useExists=false;
静态价值;
如果($useExists){
//使用以前对此函数调用的值
//
$useExists=false;
返回$useValue;
}否则{
//Box-Muller变换的极性形式
//
$w=2.0;
而($w>=1.0)| |($w==0.0)){
$x=随机_PN();
$y=随机_PN();
$w=($x*$x)+($y*$y);
}
$w=sqrt((-2.0*log($w))/$w);
//设置下一次调用此函数的值
//
$useValue=$y*$w;
$useExists=true;
返回$x*$w;
}
}
函数gauss_ms($mean,
$stddev)
{
//调整我们的高斯随机数以适应平均值和标准偏差
//除以4是一个任意值,有助于拟合分布
//在我们要求的范围内,并提供了$STDEV=1.0的最佳匹配
//
返回高斯()*($stddev/4)+$mean;
}
函数GaussianWarghtDRND($LowValue,
$maxRand,
$mean=0.0,
$STDEV=2.0)
{
//调整高斯随机值,使其符合我们指定的范围
//通过“修剪”极值作为分布曲线
//接近+/-无穷远
$rand_val=$LowValue+$maxRand;
而($rand_val<$LowValue)| |($rand_val>=($LowValue+$maxRand))){
$rand_val=地板(高斯m($mean,$stddev)*$maxRand)+$LowValue;
$rand_val=($rand_val+$maxRand)/2;
}
返回$rand_val;
}
函数bellWeightedRnd($LowValue,
$maxRand)
{
返回gaussianwarthdrnd($LowValue,$maxRand,0.0,1.0);
}
对于简单的bell分布,只需使用最小值和最大值调用bellWeightedRnd();对于更复杂的分布,GaussianWarghtedRnd()还允许您指定分布的平均值和stdev


高斯贝尔曲线非常适合IQ分布,尽管我也有类似的程序用于替代分布曲线,如泊松分布、伽马分布、对数分布和c分布。

它可能看起来既长又复杂(并且是为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 ) ;
}
函数随机_0_1()
{
//使用mt_rand()返回随机数,平均分布范围为0到1(含0到1)
//
返回(浮动)mt_rand()/(浮动)mt_getrandmax();
}
函数随机_PN()
{
//使用mt_rand()返回随机数,平均分布范围为-1到1(含1)
//
返回值(2.0*random_0_1())-1.0;
}
函数gauss()
{
静态$useExists=false;
静态价值;
如果($useExists){
//使用以前对此函数调用的值
//
$useExists=false;
返回$useValue;
}否则{
//Box-Muller变换的极性形式
//
$w=2.0;
而($w>=1.0)| |($w==0.0)){
$x=随机_PN();
$y=随机_PN();
$w=($x*$x)+($y*$y);
}
$w=sqrt((-2.0*log($w))/$w);
//设置下一次调用此函数的值
//
$useValue=$y*$w;
$useExists=true;
返回$x*$w;
}
}
函数gauss_ms($mean,
$stddev)
{
//调整我们的高斯随机数以适应平均值和标准偏差
//除以4是一个任意值,有助于拟合分布
//在我们要求的范围内,并提供了$STDEV=1.0的最佳匹配
//
返回高斯()*($stddev/4)+$mean;
}
函数GaussianWarghtDRND($LowValue,
$maxRand,
$mean=0.0,
$stddev=2。