Answered by : mangaliso-earnshaw
import java.util.Random;
Random rand = new Random();
// Obtain a number between [0 - 49].
int n = rand.nextInt(50);
// Add 1 to the result to get a number from the required range
// (i.e., [1 - 50]).
n += 1;
Source : https://stackoverflow.com/questions/5887709/getting-random-numbers-in-java | Last Update : Mon, 16 Mar 20
Answered by : mitrofanos-ntatidis
import java.util.Random;
Random rand = new Random();
int random_integer = rand.nextInt(upperbound-lowerbound) + lowerbound;
Source : https://stackoverflow.com/questions/11743267/get-random-numbers-in-a-specific-range-in-java | Last Update : Thu, 28 May 20
Answered by : joseph-sanders
import java.util.Random;
public class Example { public static void main(String[] args) { Random rd = new Random(); // creating Random object System.out.println(rd.nextBoolean()); // displaying a random boolean }
}
Source : https://www.tutorialspoint.com/generate-random-boolean-in-java | Last Update : Tue, 19 May 20
Answered by : exuberant-eagle-qeo50utovxrx
(int)(Math.random() * ((max - min) + 1)) + min
Copy
Source : https://mkyong.com/java/java-generate-random-integers-in-a-range/ | Last Update : Sat, 28 Mar 20
Answered by : xyndra
// In Java 1.7 or later, the standard way to do this is as follows:
import java.util.concurrent.ThreadLocalRandom;
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
// Example for a number dice-like output:
int randomNum = ThreadLocalRandom.current().nextInt(1, 6 + 1);
Source : https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java | Last Update : Sat, 08 Oct 22
Answered by : nitbit25
import java.util.Random;
class scratch{ public static void main(String[] args) { Random rand = new Random(); System.out.println( rand.nextInt(5) ); //prints a random Int between 0 - 4 (including 0 & 4); }
}
Source : | Last Update : Wed, 15 Jan 20
Answered by : saldainis
int min = 9999;
int max = 1000;
//Generate random int value from 10000 to 99999
//System.out.println("Random value in int from "+min+" to "+max+ ":");
int random_int = (int)(Math.random() * (max - min + 1) + min);
Source : | Last Update : Fri, 13 May 22
Answered by : scary-seal-q4wb7yw7q3ok
Min + (int)(Math.random() * ((Max - Min) + 1))
Source : https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java | Last Update : Wed, 06 Jan 21
Answered by : gprata
import java.util.concurrent.ThreadLocalRandom;
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
Source : https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java | Last Update : Mon, 02 Nov 20
Answered by : clumsy-crossbill-x3fa1qvebs6m
Min + (int)(Math.random() * ((Max - Min) + 1))
Source : https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java | Last Update : Fri, 25 Feb 22
Answered by : impossible-impala-gp45r1f4ycvw
int rand = ThreadLocalRandom.current().nextInt(x,y);
Source : | Last Update : Tue, 19 May 20