Android examples for java.util:Random Integer
Returns a random long number in the given range [0, n], with both inclusive.
//package com.java2s; import java.util.Random; public class Main { /**// w ww .j a v a2 s . c o m * Returns a random long number in the given range [0, n], with both inclusive. * <br> * If negative value is specified, then range is between [n, 0]. * <br> * Internally calls {@link #getRandomLongWithinRange(long, long)}. * <br> * E.g., * @param max * @return */ public static long getRandomLong(long max) { long min = 0; if (max < 0) { long temp = max; max = 0; min = temp; } return getRandomLongWithinRange(min, max); } /** * @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)); } }