Like this code?
Show the author your appreciation.
This basically randomly 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.
Here is how it works:
1. Subtract the minimal value from the maximal value
2. Generate the numbers for the digits by using rand()
3. Combine the digits
4. Check if it is too large or not, if is, redo 2, if not,go on.
5. Add the minimal value to the random value.
In worst case, by chance, 10 random number need to be generated before we get a good one. Numbers like 100000, 10000.
In best case, only one will be generated, numbers like 99999, 9999
<?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 );
}
?>
Here is an example of useage:
<?php
//Note, the arguments are strings
//that represent numbers, not numbers
bcrand ( '0' , '9999999999999' );
?>
Fast PI calculator. Can easily find the 1000th decimal place of pi in 5 seconds. Categories : PHP , BC math , Algorithms minus - subtract arrays. Send two arrays and get an array with the operation A-B, elements on A that are not included on B. Categories : PHP , Arrays , Algorithms IPhider Obscure Any URL Anonymity connection lores obfuscation corporate survival. Categories : PHP , Algorithms , Security , URLs Browse a MySQL database & draw a tree view & load final items into a template page. Categories : MySQL , Complete Programs , Algorithms , PHP , Databases Recursive function to move files on a filesystem. It can be minor changed in order to copy recursively.
Categories : PHP , Filesystem , Algorithms quick sort for associative arrays Categories : Algorithms , Arrays , PHP Output database records in multiple table columns.
multiple, table, columns, output, format, query output, html table, php Categories : General SQL , Algorithms , PHP , MySQL Function to convert Arabic numbers into Roman Numerals Categories : Algorithms , PHP , Date Time calculus of the eastersunday Categories : BC math , PHP , Date Time , Databases How to Generate a random 8 character string in php3? Categories : PHP , Algorithms Library of math functions to expand the functionality of PHP3. Version 1.2.1 fixes a major problem with the gcd function.
Categories : Algorithms , PHP , Math. Object() = Custom __autoload + Singleton. "automagically" instantiates a class and always retuns the same instance of the same class. It's pretty useful when you want to have persistence in objects. Categories : PHP , PHP Classes , Algorithms Dollar Serial Number Validator Categories : PHP , Security , Algorithms Credit Card Identification and Validation Class - The credit_card class provides methods for cleaning, validating and identifying the type of credit card numbers. Categories : PHP , PHP Classes , Credit Cards , Ecommerce , Algorithms Boolean Keyword Interpreter Categories : PHP , Algorithms , Search Engines