Here you can find the source of getRandomInteger(int aStart, int aEnd)
public static int getRandomInteger(int aStart, int aEnd)
//package com.java2s; //License from project: Apache License import java.util.Random; public class Main { public static int getRandomInteger(int aStart, int aEnd) { if (aStart > aEnd) { throw new IllegalArgumentException("Start cannot exceed End."); }// w w w.j a v a 2 s . c o m Random aRandom = new Random(); //get the range, casting to long to avoid overflow problems long range = (long) aEnd - (long) aStart + 1; // compute a fraction of the range, 0 <= frac < range long fraction = (long) (range * aRandom.nextDouble()); int randomNumber = (int) (fraction + aStart); System.out.printf("Generated : " + randomNumber); return randomNumber; } }