Returns a random number between MIN inclusive and MAX inclusive. - Android java.util

Android examples for java.util:Random

Description

Returns a random number between MIN inclusive and MAX inclusive.

Demo Code


//package com.java2s;

import java.util.Random;

public class Main {
    /**//from ww w.j a v a  2s.  c  o m
     * Returns a random number between MIN inclusive and MAX inclusive.
     * <br>
     * E.g., 
     * @param min
     *            value inclusive
     * @param max
     *            value inclusive
     * @return an int between MIN inclusive and MAX exclusive.
     */
    public static int getRandomIntegerWithinRange(int min, int max) {
        if (min == max) {
            return max;
        }
        if (max < min) {
            int temp = max;
            max = min;
            min = temp;
        }
        Random r = new Random();
        return r.nextInt(max - min + 1) + min;
    }
}

Related Tutorials