Works great and fast. I can't find any way to optimize it.
PHP Code:
function bcrand($min, $max){
if(bccomp($max,$min)!=1){
return 0;
}
$top = bcsub($max,$min);
$rand = bcadd($top, 1);
$length = strlen($top);
while(bccomp($rand,$top)==1){
unset($rand_part);
$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;
}
}
return bcadd($rand,$min,0);
}