how to create a random number in java

Java
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;public class GuessTheNumber {
  public static void main(String[] args) {
    
    
    int randomInt = (int)(20.0 * Math.random());
    int num1 = randomInt;
    int num2 = 10;
    
    if(num1 == num2){
    	System.out.println("Correct!");
    } else if(num1 > num2){
    	System.out.println("Too low!");
    } else{
    	System.out.println("Too high!");
    }
    
    
  }
}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

Also in Java: