WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : BCrand, a function returns large random numbers
Categories : PHP, BC math, Algorithms Click here to Update Your Picture
Chao Xu
Date : Feb 14th 2007
Grade : 1 of 5 (graded 1 times)
Viewed : 1384
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Chao Xu
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

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
Check parameters validity. Paranoia was designed to check the validity of the parameters that a php page will receive after a form submission. It can be used to check the variables sent by POST or GET
Categories : Algorithms, HTML and PHP, PHP, Variables
A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL
Boolean Keyword Interpreter
Categories : PHP, Algorithms, Search Engines
Diffusion-Limited Aggregation visualization
Categories : PHP, Graphics, Algorithms, Math.
bcadd -- Add two arbitrary precision numbers
Categories : PHP, PHP Functions, BC math
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
calculus of the eastersunday
Categories : BC math, PHP, Date Time, Databases
Mail-lib provides a simple interface to the sendmail program. Note: you must actually have sendmail on your machine (sorry windows NT users).
Categories : Algorithms, Email, PHP
A recursive function to traverse a multi-dimensional array where the dimensions are not known
Categories : Arrays, PHP, Algorithms
The Porter Word Stemming Algorithm in PHP Reduces words to their base stem for search engines and indexing
Categories : Algorithms, PHP, Strings
SHA: Implementation of the Secure Hash Algorithm in pure PHP. This is a secure one-way function that can be used to perform challenge response login algorithms over an insecure connection.
Categories : Algorithms, PHP, Security
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
Calculate Body Mass Index
Categories : PHP, Algorithms, Regexps