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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

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

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to generate random integers between specific range in Java

5 Answers

0 votes
import java.util.Random;
 
public class MyClass
{
    public static int generateRandomInteger(int min, int max) {
        return new Random().nextInt(max - min + 1) + min;
    }
    
    public static void main(String[] args)
    {
        int min = 4, max = 11;
 
        for (int i = 0; i < 10; i++) {
            System.out.println(generateRandomInteger(min, max));
        }
    }
}





/*
run:

10
8
6
5
9
11
7
11
4
8

*/

 



answered Mar 18, 2023 by avibootz
0 votes
import java.util.Random;
 
public class MyClass
{
    public static int generateRandomInteger(int min, int max) {
        Random rand = new Random();
 
        return rand.ints(min, (max + 1))    
                    .findFirst()            
                    .getAsInt();            
    }
    
    public static void main(String[] args)
    {
        int min = 4, max = 11;
 
        for (int i = 0; i < 10; i++) {
            System.out.println(generateRandomInteger(min, max));
        }
    }
}





/*
run:

10
8
9
8
8
6
4
5
11
9

*/

 



answered Mar 18, 2023 by avibootz
0 votes
import java.util.Random;
  
public class MyClass
{
    public static int generateRandomInteger(int min, int max) {
        return min + (int)(Math.random() * ((max - min) + 1));
    }
     
    public static void main(String[] args)
    {
        int min = 4, max = 11;
  
        for (int i = 0; i < 10; i++) {
            System.out.println(generateRandomInteger(min, max));
        }
    }
}
 
 
 
 
 
/*
run:
 
8
9
11
11
6
7
10
4
4
11
 
*/

 



answered Mar 18, 2023 by avibootz
0 votes
import java.util.concurrent.ThreadLocalRandom;
  
public class MyClass
{
    public static int generateRandomInteger(int min, int max) {
        return ThreadLocalRandom.current().nextInt(min, max + 1);
    }
     
    public static void main(String[] args)
    {
        int min = 4, max = 11;
  
        for (int i = 0; i < 10; i++) {
            System.out.println(generateRandomInteger(min, max));
        }
    }
}
 
 
 
 
 
/*
run:
 
6
11
4
8
4
8
6
7
5
11
 
*/

 



answered Mar 18, 2023 by avibootz
0 votes
import java.security.SecureRandom;
  
public class MyClass
{
    public static int generateRandomInteger(int min, int max) {
        SecureRandom random = new SecureRandom();
        random.setSeed(random.generateSeed(20)); // 20 bytes
 
        return random.nextInt((max - min) + 1) + min;
    }
     
    public static void main(String[] args)
    {
        int min = 4, max = 11;
  
        for (int i = 0; i < 10; i++) {
            System.out.println(generateRandomInteger(min, max));
        }
    }
}
 
 
 
 
 
/*
run:
 
8
5
11
5
6
6
8
6
4
10
 
*/

 



answered Mar 18, 2023 by avibootz

Related questions

3 answers 266 views
1 answer 168 views
2 answers 218 views
2 answers 158 views
1 answer 111 views
1 answer 186 views
...