PHP

A Function Returns Large Random Numbers

CK CK 3788 
Created at  
427 0 0
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.

<?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);
}
?>

Tags PHP Random Random Number bcadd bcsub count rand Facebook X
Comments 0
Similar posts
  1. Function AccountCryptPassword($password,$salt)
    725
  2. Number Format
    482
  3. Calculator with basic four functions
    467
  4. Array_count_values
    504
  5. Send email Message With PHP
    697
  6. Cardinal Endings
    490
  7. URL Rewriter PHP
    614
  8. The three simplest regular expressions you will use in PHP
    580
  9. Adding and Subtracting Time
    556
  10. How to remove or set time limit that defined as max_execution_time in php.ini
    570
  11. How to convert formatted date time string to datetime?
    446
  12. How to get Referral URL ?
    2,072
  13. PHP form handling example in single page
    415
  14. PHP if...else... example
    445
  15. How to include a file with error reporting ?
    428
  16. How to check if a file exists ?
    786
  17. How to force cookies, disable php sessid in URL ?
    554
  18. How to make random number ?
    517
  19. form action - detect form post
    428
  20. for loop - simple
    449
  21. How to ban / block IP addresses
    509
  22. email validation, simple
    412
  23. How to get current filename as variable ?
    768
  24. How to redirect browser in PHP ?
    482
  25. PHP date and time formatting
    428
  26. How to declare array in PHP like C/C++
    670
  27. How to pass variables via url
    468
  28. How to send email in PHP ?
    889
  29. PHP Switch Example
    427
  30. Can PHP switch in strings ?
    391
  1. How to force cookies, disable php sessid in URL ?
    554
  2. How to check if a file exists ?
    786
  3. How to include a file with error reporting ?
    428
  4. PHP if...else... example
    445
  5. PHP form handling example in single page
    415
  6. How to get Referral URL ?
    2,072
  7. How to convert formatted date time string to datetime?
    446
  8. How to remove or set time limit that defined as max_execution_time in php.ini
    570
  9. Adding and Subtracting Time
    556
  10. The three simplest regular expressions you will use in PHP
    580
  11. Rotate Bitmap (any angle, any center of rotation)
    404
  12. Send email Message With PHP
    697
  13. Array_count_values
    504
  14. Calculator with basic four functions
    467
  15. Number Format
    482
  16. Function AccountCryptPassword($password,$salt)
    725
  17. Cardinal Endings
    490
  18. URL Rewriter PHP
    614
  19. Browser detection in PHP
    736
  20. Generate a random string in PHP
    5,607
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,711
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    85
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    198
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    509
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    232
  17. The Concept of Scaffolding in AI Agents
    221
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    576
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    253
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    327
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252