Random Number Generator in Java

To generate a random number in Java, you can use the Random class provided by the java.util package. Here's a simple example to generate a random integer between 1 and 10, inclusive:


                 
                import java.util.Random;

                public class Main {
                    public static void main(String[] args) {
                        Random rand = new Random(); // Create a Random object
                        int randomNum = rand.nextInt(10) + 1; // Generate a random number between 1 and 10
                        System.out.println("Random Number: " + randomNum);
                    }
                } 
                               
                

In this code:

Preset number shortcuts between

Digit shortcuts

Code samples