Java Program to Guess a Random Number in a Range Last Updated : 04 Dec, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Write a program that generates a random number and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high, try again. If the user’s guess is lower than the random number, the program should display Too low, try again. The program should use a loop that repeats until the user correctly guesses the random number. Input: 15 (a random value that is not known before) Output: Guess a number between 1 and 100: 1 Too low, try again Guess a number between 1 and 100: 10 Too low, try again Guess a number between 1 and 100: 25 Too high, try again Guess a number between 1 and 100: 20 Too high, try again Guess a number between 1 and 100: 15 Yes, you guessed the number. Example Java // Java Program to guess a Random Number Generation import java.util.Random; import java.util.Scanner; public class GFG { public static void main(String[] args) { // stores actual and guess number int answer, guess; // maximum value is 100 final int MAX = 100; // takes input using scanner Scanner in = new Scanner(System.in); // Random instance Random rand = new Random(); boolean correct = false; // correct answer answer = rand.nextInt(MAX) + 1; // loop until the guess is correct while (!correct) { System.out.println( "Guess a number between 1 and 100: "); // guess value guess = in.nextInt(); // if guess is greater than actual if (guess > answer) { System.out.println("Too high, try again"); } // if guess is less than actual else if (guess < answer) { System.out.println("Too low, try again"); } // guess is equal to actual value else { System.out.println( "Yes, you guessed the number."); correct = true; } } System.exit(0); } } Output Comment More infoAdvertise with us Next Article Java Program to Generate Random Hexadecimal Bytes P pawki Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads How to Add Random Number to an Array in Java? To generate an array of integers with random values the nextInt() method from the java.util.Random class is used. From the random number generator sequence, this method returns the next random integer value.Assigning a Random Value to an ArrayWe can assign random values to an array by two approaches 2 min read Java Program to Implement Inversion Method for Random Number Generation Here we will be going through the inversion method for random number generation in java. So basically we will be illustrating two approaches which are as follows: Shuffling elements in an arrayUsing Collection.shuffle() method Important Method here namely is setSeed(). It is used to set the seed via 9 min read Java Program to Implement Park-Miller Random Number Generation Algorithm ParkâMiller random number generator is also known as Lehmer random number generator. A general formula of a random number generator (RNG) of this type is, Xk+1 = a * xk mod m Where the modulus m is a prime number or a power of a prime number, the multiplier a is an element of high multiplicative ord 3 min read Number Guessing Game in Java A number-guessing game in Java is a simple program where the computer randomly selects a number, and the user has to guess it within a limited number of attempts. The program provides feedback on whether the guessed number is too high or too low, guiding the user toward the correct answer.This proje 4 min read Java Program to Generate Random Hexadecimal Bytes To generate Random Hexadecimal Bytes, first, a random byte can be generated in decimal form using Java.util.Random.nextInt() and then it can be converted to hexadecimal form using Integer.toHexString() method. 1. Java.util.Random.nextInt() The nextInt() method is used to obtain the next integer from 2 min read Java Program to Implement the Linear Congruential Generator for Pseudo Random Number Generation Linear Congruential Method is a class of Pseudo-Random Number Generator (PRNG) algorithms used for generating sequences of random-like numbers in a specific range. This method can be defined as: Xi+1 = aXi + c mod m where, X, is the sequence of pseudo-random numbers m, ( > 0) the modulus a, (0, m 3 min read Like