Question

In: Computer Science

Write a Java program to generate random numbers in the following range a. 1 <=n <=...

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

Solutions

Expert Solution

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:-


Related Solutions

java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. 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 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
For this problem, you will write a program using two queues. Generate n random numbers between...
For this problem, you will write a program using two queues. Generate n random numbers between 10 and 100 (both inclusive), where n>9. The value of n should be taken as input from the user and n should be >9. The numbers could be duplicated. Enqueue all these numbers to the first queue. The objective is to find the numbers whose sum of digits is odd and enqueue them to the second queue. The remaining numbers (whose sum of digits...
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1 to 10 then subtract the second integer from the first integer. Note, if the second integer is greater than the first integer, swap the two integers before making the subtraction.Then prompt user for the answer after the subtraction. If the answer is correct, display “Correct”otherwise display “Wrong”.You will need to do this in a loop FIVE times and keep a count of how many...
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Write a simple java program to list roman numeral for a given range of numbers. Roman...
Write a simple java program to list roman numeral for a given range of numbers. Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M. I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000 Roman numerals are usually written largest to smallest from left to right. But a number like 4 is not written as IIII. It is written as IV. Because...
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the reverse order. Using a ListItreator output the contents of the LinkedList in the original order.
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the original order. Using a ListItreator output the contents of the LinkedList in the reverse order.
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal...
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal to n, such that the digit to the right is greater than the left digit (ai+1 > ai). E.g. if n=3 (123,124,125,……129,234,…..789)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT