1import java.util.Random;
2
3Random rand = new Random();
4
5// Obtain a number between [0 - 49].
6int n = rand.nextInt(50);
7
8// Add 1 to the result to get a number from the required range
9// (i.e., [1 - 50]).
10n += 1;
1import java.util.Random;
2
3Random rand = new Random();
4int maxNumber = 10;
5
6int randomNumber = rand.nextInt(maxNumber) + 1;
7
8System.out.println(randomNumber);
1// Thread safe random number generator.
2
3import java.util.concurrent.ThreadLocalRandom;
4
5
6
7// Generate random integers
8int val = ThreadLocalRandom.current().nextInt();
9
10// Print random int
11System.out.println("Random Integer: " + val);
12
1import java.util.Random;
2class GenerateRandom {
3 public static void main( String args[] ) {
4 Random rand = new Random(); //instance of random class
5 int upperbound = 25;
6 //generate random values from 0-24
7 int int_random = rand.nextInt(upperbound);
8 double double_random=rand.nextDouble();
9 float float_random=rand.nextFloat();
10
11 System.out.println("Random integer value from 0 to" + (upperbound-1) + " : "+ int_random);
12 System.out.println("Random float value between 0.0 and 1.0 : "+float_random);
13 System.out.println("Random double value between 0.0 and 1.0 : "+double_random);
14 }
15}