Generates random integer from a range - Java java.util

Java examples for java.util:Random Int

Description

Generates random integer from a range

Demo Code


//package com.java2s;

import java.util.Random;

public class Main {
    public static void main(String[] argv) throws Exception {
        int min = 2;
        int max = 2;
        System.out.println(getRandomNum(min, max));
    }/* w ww. jav  a  2 s. c  o  m*/

    /**
     * Generates random integer from a range
     * 
     * @param min
     * @param max
     * @return - random integer
     */
    public static int getRandomNum(int min, int max) {
        return new Random().nextInt((max - min) + 1) + min;
    }
}

Related Tutorials