Randomly chooses from a uniform distribution of integers in the range [i, j) - Java java.util

Java examples for java.util:Random

Description

Randomly chooses from a uniform distribution of integers in the range [i, j)

Demo Code


//package com.java2s;

import java.util.Random;

public class Main {
    /**//  w  ww.ja  v  a 2s .  c  o m
     * Randomly chooses from a uniform distribution of integers in the range [i, j)
     * @param random random number generator
     * @param i beginning of the range (inclusive)
     * @param j ending of the range (exclusive)
     * @return random integer between i and j
     */
    public static final int sampleUniformDistribution(Random random, int i,
            int j) {
        if (i >= j)
            throw new IllegalArgumentException();
        else
            return i + random.nextInt(j - i);
    }
}

Related Tutorials