Java examples for java.util:Random
Generates pseudo-random integer from specific range.
//package com.java2s; public class Main { /**//from w w w .j a v a2 s . c o m * Generates pseudo-random integer from specific range. Generated number is * great or equals to min parameter value and less then max parameter value. * Uses {@link Math#random()}. * * @param min * lower (inclusive) boundary * @param max * higher (exclusive) boundary * * @return pseudo-random value */ public static int randomInt(int min, int max) { return min + (int) (Math.random() * (max - min)); } /** * Returns a <code>double</code> value with a positive sign, greater than or * equal to <code>0.0</code> and less than <code>1.0</code>. Returned values * are chosen pseudorandomly with (approximately) uniform distribution from * that range. * * <p> * When this method is first called, it creates a single new * pseudorandom-number generator, exactly as if by the expression * <blockquote> * * <pre> * new java.util.Random * </pre> * * </blockquote> This new pseudorandom-number generator is used thereafter * for all calls to this method and is used nowhere else. * </p> * * <p> * This method is properly synchronized to allow correct use by more than * one thread. However, if many threads need to generate pseudorandom * numbers at a great rate, it may reduce contention for each thread to have * its own pseudorandom-number generator. * </p> * * @return a pseudorandom <code>double</code> greater than or equal to * <code>0.0</code> and less than <code>1.0</code>. * * @see java.util.Random#nextDouble() */ public static double random() { return Math.random(); } }