get Random Long Within Range - Android java.util

Android examples for java.util:Random Integer

Description

get Random Long Within Range

Demo Code


//package com.java2s;

import java.util.Random;

public class Main {
    /**//from   w  w  w  .j  a  v a2 s.  com
     * @param min
     * @param max
     * @return
     */
    public static long getRandomLongWithinRange(long min, long max) {
        if (min == max) {
            return max;
        }
        if (max < min) {
            long temp = max;
            max = min;
            min = temp;
        }
        Random r = new Random();
        double randomFactor = r.nextDouble();
        return (long) (min + ((max - min + 1) * randomFactor));
    }
}

Related Tutorials