Re: İlgi: a for loop and probability random all i need is make them to reverse

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2008/11/22 Tontonq Tontonq <rootdot@xxxxxxxxx>:
> sorry i think i did tell u wrong about it problem is not showing name
> of it as exaclty i just wanted give a point to it thats not real
> problem real problem is i want it be more unpossible when i give the
> class's add function's higher value

In that case I would go for an algorithm using some kind of gaussian
distribution, like the Marsaglia polar method [1].
I once wrote something similar to what (I think) you are asking for ...

<?php
function marsaglia($likelihood) { // adapted polar-method by marsaglia
    if (!is_numeric($likelihood) || ($likelihood == 0)) $likelihood = 100;
    elseif ($likelihood <= 1) return 1;
    // the greater likelihood, the less the chance of getting the thingy
    $v = 2;
    $u1 = $u2 = 1;
    while ($v > 1) {
        $u1 = 1/(rand(1, $likelihood)*$likelihood);
        $u2 = 1/(rand(1, $likelihood)*$likelihood);

        $v = $u1*$u1 + $u2*$u2;
    }

    $p = $u1*sqrt((-2)*(log($v)/$v));
    return ($p < 0.85) ? 1 : (int)$p;
}
?>

The best return of this function would be 1. Now if you call the
function with a high $likelihood (say 1000 and more) it gets less
likely have 1 returned ( gaussian ).
If you want to decrease the chance of getting values close to 1 then
you will have to increase $likelihood inside the function or
something.

[1] http://en.wikipedia.org/wiki/Marsaglia_polar_method

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux