Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,401 questions

56,365 answers

573 users

How to generate an 8 digits random number in PHP

3 Answers

0 votes
define("N", 20);

for ($i = 0; $i < N; $i++) {
    // random_int($min, $max) is cryptographically secure and inclusive on both bounds
    echo random_int(10000000, 99999999) . "\n";
}



/*
run: 
 
74366892
32253243
40758476
84431840
17193137
94607487
81920959
21115423
29252469
65698958
97904273
83522507
58069964
82885611
60188901
13164209
92763276
48950332
12239536
79729871
 
*/

 



answered Apr 28, 2016 by avibootz
edited 3 days ago by avibootz
0 votes
define("N", 20);

for ($i = 0; $i < N; $i++) {
    // Generates [0, 99999999] and zero-pads to 8 digits (e.g., "00412983")
    $code = sprintf("%08d", random_int(0, 99999999));
    echo $code . "\n";
}


/*
run: 
 
51761547
43709421
24332648
54059023
21432391
74786276
36269192
22161097
32222591
99142515
97907026
25911099
23493055
88125978
08687076
27328540
15119801
80307104
98108408
64337923
 
*/

 



answered 3 days ago by avibootz
0 votes
declare(strict_types=1);

/**
 * Generates a random 8-digit integer between 10,000,000 and 99,999,999 (inclusive).
 * Guaranteed to start with a non-zero digit (1-9).
 *
 * @return int An 8-digit positive integer.
 * @throws Exception If a cryptographically secure source of randomness is unavailable.
 */
function generate8DigitNumber(): int
{
    // random_int($min, $max) is cryptographically secure and inclusive on both bounds.
    return random_int(10_000_000, 99_999_999);
}

/**
 * Generates an 8-character numeric string code between "00000000" and "99999999".
 * Preserves leading zeros using sprintf, suitable for OTPs, PINs, or serial numbers.
 *
 * @return string An 8-character numeric string with leading zero padding if needed.
 * @throws Exception If a cryptographically secure source of randomness is unavailable.
 */
function generate8DigitCode(): string
{
    // Generate a value in the range [0, 99999999]
    $rawValue = random_int(0, 99_999_999);

    // Format as an 8-digit decimal string with leading zeros (%08d)
    return sprintf('%08d', $rawValue);
}

// ==========================================
// Example Execution
// ==========================================

try {
    // 1. Generate an 8-digit mathematical integer [10000000 to 99999999]
    $randomNumber = generate8DigitNumber();
    echo 'Generated 8-digit integer: ' . $randomNumber . PHP_EOL;

    // 2. Generate an 8-character string code [00000000 to 99999999] preserving leading zeros
    $randomCode = generate8DigitCode();
    echo 'Generated 8-digit code string: ' . $randomCode . PHP_EOL;

} catch (Exception $e) {
    // Exception thrown if OS lacks an adequate source of entropy
    error_log('Error generating random number: ' . $e->getMessage());
}


/*
run: 
 
Generated 8-digit integer: 59759711
Generated 8-digit code string: 33012698
 
*/

 



answered 3 days ago by avibootz
...