Java Random Int getRandomInteger(int aStart, int aEnd)

Here you can find the source of getRandomInteger(int aStart, int aEnd)

Description

get Random Integer

License

Apache License

Declaration

public static int getRandomInteger(int aStart, int aEnd) 

Method Source Code

//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;
    }
}

Related

  1. getRandomInt(int size)
  2. getRandomInt(Random rand, int lower, int upper)
  3. getRandomInt(String level)
  4. getRandomIntBetween(int low, int high)
  5. getRandomIntBetween(int mi, int ma)
  6. getRandomInteger(int min, int max)
  7. getRandomIntegerArray(Random rand, int size, int range)
  8. getRandomIntegerInRange(Random p_76136_0_, int p_76136_1_, int p_76136_2_)
  9. getRandomIntegerInRange(Random random, int i, int j)