'bcadd'에 해당되는 글 1건

  1. 2007/08/26 A Function Returns Large Random Numbers
2007/08/26 01:54

A Function Returns Large Random Numbers

This basicallyrandomly pick a number between the min and max values. This is made because the rand() and mt_rand() function can not accept numbers larger than 2^31 or 2^63,depend on your processor and complier.

<?php
function bcrand($min,$max){
    bcscale(0);
    if(bccomp($max,$min)!=1){
        return 0;
    }
    $top =bcsub($max,$min);
    $rand =bcadd($top,1);
    $length = strlen($top);
   
        $n = 0;
        while(9*$n <= $length){
            if($length - 9*$n >= 9){
                $rand_part[] = rand(0,999999999);
            }else{
                $j = 0; $foo = '';
                while($j < $length-9*$n){
                    $foo .= '9';
                      $j;
                }
                $foo  = 0;
                $rand_part[] = rand(0,$foo);
            }
              $n;
        }
        $i = 0;
        $rand ='';
        count = count($rand_part);
        while($i < $count){
            $rand .= $rand_part[$i];
              $i;
        }
    while(bccomp($rand,$top)==1){
        $rand = substr($rand,1,strlen($rand)).rand(0,9);
    }
    return bcadd($rand,$min);
}
?>

Trackback 2 Comment 0