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 program to simulate a calculator. Generate two random numbers (between 1-15) and an ask...
Write a program to simulate a calculator. Generate two random numbers (between 1-15) and an ask the user for an arithmetic operator. Using a switch statement, your program has to perform the arithmetic operation on those two operands depending on what operator the user entered. Ask the user if he/she wants to repeat the calculations and if the answer is yes or YES, keep repeating. If the answer is something else, then stop after the first calculation. c++
Write a C++ program to generate two random numbers (Rnd1 and Rnd2). These two numbers should...
Write a C++ program to generate two random numbers (Rnd1 and Rnd2). These two numbers should be within a range [100, 500]. If Rnd1 greater than or equals to Rnd2, print out the result of (Rnd1-Rnd2). Otherwise, print out the result of (Rnd2-Rnd1). In all cases, print Rnd1, Rnd2, and the results of subtractions in suitable messages.
For this assignment, write a program that will generate three randomly sized sets of random numbers...
For this assignment, write a program that will generate three randomly sized sets of random numbers using DEV C++ To use the random number generator, first add a #include statement for the cstdlib library to the top of the program: #include <cstdlib> Next, initialize the random number generator. This is done by calling the srand function and passing in an integer value (known as a seed value). This should only be done ONE time and it MUST be done before...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT