In: Computer Science
Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5
JAVA PROGRAMMING
Ans:-
(a) 1 <=n <= 3
Code:-
import java.util.concurrent.ThreadLocalRandom;
public class RandomGen
{
        public static void main (String[] args) throws java.lang.Exception
        {
                int min =1,max=3; //initialize min and max variable with range given
                //call getRandomInRange method to generate Random number
            int rand = getRandomInRange(min,max);       
            //print Random number
            System.out.println("Random number between range "+min+" and "+max+" : "+rand);
        } 
         public static int getRandomInRange(int min, int max) 
    { 
        //call nextInt method of ThreadLocalRandom class and pass min and max as a parameter to it
        //generate random number between 1 and 3
        return ThreadLocalRandom.current().nextInt(min, max + 1); 
    } 
}
Output:-

(b) 1 <= n <= 200
Code:-
import java.util.concurrent.ThreadLocalRandom;
public class RandomGen
{
        public static void main (String[] args) throws java.lang.Exception
        {
                int min =1,max=200; //initialize min and max variable with range given
                //call getRandomInRange method to generate Random number
            int rand = getRandomInRange(min,max);       
            //print Random number
            System.out.println("Random number between range "+min+" and "+max+" : "+rand);
        } 
         public static int getRandomInRange(int min, int max) 
    { 
        //call nextInt method of ThreadLocalRandom class and pass min and max as a parameter to it
        //generate random number between 1 and 200
        return ThreadLocalRandom.current().nextInt(min, max + 1); 
    } 
}
Output:-

(c) 0 <= n <= 9
Code:-
import java.util.concurrent.ThreadLocalRandom;
public class RandomGen
{
        public static void main (String[] args) throws java.lang.Exception
        {
                int min =1,max=9; //initialize min and max variable with range given
                //call getRandomInRange method to generate Random number
            int rand = getRandomInRange(min,max);       
            //print Random number
            System.out.println("Random number between range "+min+" and "+max+" : "+rand);
        } 
         public static int getRandomInRange(int min, int max) 
    { 
        //call nextInt method of ThreadLocalRandom class and pass min and max as a parameter to it
        //generate random number between 1 and 9
        return ThreadLocalRandom.current().nextInt(min, max + 1); 
    } 
}
Output:-

(d) 1000 <= n <= 2112
Code:-
import java.util.concurrent.ThreadLocalRandom;
public class RandomGen
{
        public static void main (String[] args) throws java.lang.Exception
        {
                int min =1000,max=2112; //initialize min and max variable with range given
                //call getRandomInRange method to generate Random number
            int rand = getRandomInRange(min,max);       
            //print Random number
            System.out.println("Random number between range "+min+" and "+max+" : "+rand);
        } 
         public static int getRandomInRange(int min, int max) 
    { 
        //call nextInt method of ThreadLocalRandom class and pass min and max as a parameter to it
        //generate random number between 1000 and 2112
        return ThreadLocalRandom.current().nextInt(min, max + 1); 
    } 
}
Output:-

(e) -1 <= n <= 5
Code:-
import java.util.concurrent.ThreadLocalRandom;
public class RandomGen
{
        public static void main (String[] args) throws java.lang.Exception
        {
                int min =-1,max=5; //initialize min and max variable with range given
                //call getRandomInRange method to generate Random number
            int rand = getRandomInRange(min,max);       
            //print Random number
            System.out.println("Random number between range "+min+" and "+max+" : "+rand);
        } 
         public static int getRandomInRange(int min, int max) 
    { 
        //call nextInt method of ThreadLocalRandom class and pass min and max as a parameter to it
        //generate random number between -1 and 5
        return ThreadLocalRandom.current().nextInt(min, max + 1); 
    } 
}
Output:-
