1. Java

Random Number Generation


How to generate random number within custom limits using Java.

import java.util.Random;

public class RandomNumberGenerator {
    public static void main(String[] args) {
        int min = 1; // Minimum value (inclusive)
        int max = 10; // Maximum value (inclusive)

        // Create an instance of the Random class
        Random random = new Random();

        // Generate a random number within the specified range
        int randomNumber = random.nextInt(max - min + 1) + min;

        // Print the generated random number
        System.out.println("Random number: " + randomNumber);

In this example, we’re using the nextInt(int bound) method from the Random class to generate a random integer within the specified range. The bound parameter is set to max - min + 1 to include both the minimum and maximum values in the range.

You can modify the min and max variables to define the desired range. Running this code will generate a random number between the specified minimum and maximum values (inclusive).