- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Here is a simple code example.
public class RandomRange { public RandomRange(){} public static long getRandomNumber(int START, int END) { Random random = new Random(); return showRandomInteger(START,END,random); } private static long showRandomInteger(int aStart, int aEnd, Random aRandom){ if ( aStart > aEnd ) { throw new IllegalArgumentException("Start cannot exceed End in RandomRange.class"); } //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()); return (fraction + aStart); } }
Comments
Post a Comment