java random numbers in specific range

htt‮ww//:sp‬w.lautturi.com
java random numbers in specific range

example 1

ThreadLocalRandom.current().nextInt(int orlautturin,int bound)

returns a pseudorandomly chosen int value between the specified orlautturin (inclusive) and the specified bound (exclusive).

/**
 * @author lautturi.com 
 * Java example:
 */

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

public class Lautturi {

	public static void main(String args[]) {

		// get a random Int between 0 - 9 (including 0 & 9);
		int randomNum = ThreadLocalRandom.current().nextInt(0,10);

		System.out.println("random integer:"+randomNum);
	}
}

example 2

int java.util.Random.nextInt(int bound)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified bound (exclusive)

/**
 * @author lautturi.com 
 * Java example: java get random number in specific range
 */

import java.util.*;

public class Lautturi {

	public static void main(String args[]) {
		
		Random rand = new Random();

		// get a random Int between 0 - 9 (including 0 & 9);
		int randomNum = rand.nextInt(10); // [0-9)

		System.out.println("random integer:"+randomNum);
	}
}
Created Time:2017-09-27 22:35:04  Author:lautturi