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,406 questions

56,397 answers

573 users

How to generate an 8 digits random number in Java

3 Answers

0 votes
import java.util.Random;
 
public class Program {
     
    public static final int N = 20;
     
    public static void main(String[] args) {
                
        Random rand = new Random(); 
 
        for (int i = 0; i < N; i++) 
            System.out.println(rand.nextInt(90000000) + 10000000);
 
        // min = 0 + 10000000 
        // max = 9000000 - 1 + 1000000 = 9999999 
    }
}


/*
run:
   
45745700
81164668
93212909
11837426
68180059
58606419
80528711
75861986
64734800
49483270
40877329
56310525
38566088
24960472
39180753
96181212
17533278
58663191
95342670
93205245
   
*/
 

 



answered Apr 28, 2016 by avibootz
edited 3 days ago by avibootz
0 votes
import java.security.SecureRandom;

/**
 * Utility class for generating random numbers.
 */
public class RandomNumberGenerator {

    // Thread-safe, cryptographically strong pseudo-random number generator.
    // Reusing a single instance avoids overhead and entropy exhaustion.
    private static final SecureRandom SECURE_RANDOM = new SecureRandom();

    // Inclusive lower and upper bounds for an 8-digit positive integer.
    private static final int LOWER_BOUND = 10_000_000;
    private static final int UPPER_BOUND = 99_999_999;

    public static void main(String[] args) {
        // Generate and display a single 8-digit random integer
        int randomNumber = generate8DigitNumber();
        System.out.println("Generated 8-digit number: " + randomNumber);

        // Optional: Generate a padded String representation (useful if zero-padding is needed)
        String randomString = generate8DigitString();
        System.out.println("Generated 8-digit string: " + randomString);
    }

    /**
     * Generates a random 8-digit positive integer between 10,000,000 and 99,999,999 (inclusive).
     * 
     * @return a random 8-digit integer
     */
    public static int generate8DigitNumber() {
        // SECURE_RANDOM.nextInt(bound) returns a value from 0 (inclusive) up to bound (exclusive).
        // Range size = (99,999,999 - 10,000,000 + 1) = 90,000,000.
        // Adding LOWER_BOUND shifts the range from [0, 89,999,999] to [10,000,000, 99,999,999].
        int range = UPPER_BOUND - LOWER_BOUND + 1;
        
        return LOWER_BOUND + SECURE_RANDOM.nextInt(range);
    }

    /**
     * Generates a random 8-digit string, allowing leading zeros (range 00000000 to 99999999).
     * Useful for verification codes, OTPs, or IDs where fixed width is required.
     * 
     * @return an 8-character numeric string
     */
    public static String generate8DigitString() {
        // Generates an integer from 0 to 99,999,999 and formats it with leading zeros
        int number = SECURE_RANDOM.nextInt(100_000_000);
        
        return String.format("%08d", number);
    }
}


/*
run:

Generated 8-digit number: 55084333
Generated 8-digit string: 95472301

*/

 



answered 3 days ago by avibootz
0 votes
import java.util.concurrent.ThreadLocalRandom;
 
public class program {
    public static int random_min_max(int min, int max) {
        return ThreadLocalRandom.current().nextInt(min, max);
    }
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(random_min_max(10000000, 100000000));
        }
    }
}
  
  
  
/*
               
run:
  
64273469
80430213
29840797
56125056
10737303
32845395
96096320
34515886
12098854
80694309
      
*/

 



answered 3 days ago by avibootz
...