View Javadoc

1   package net.sf.josceleton.commons.util;
2   
3   import java.util.Random;
4   
5   /**
6    * @since 0.4
7    */
8   public final class RandomUtil {
9   
10  	private static final Random R = new Random();
11  
12  	private RandomUtil() {
13  		// utility class is not instantiable
14  	}
15  
16  	/**
17  	 * @since 0.4
18  	 */
19  	public static int randFromZeroToExclusive(final int exclusiveMax) {
20          return R.nextInt(exclusiveMax);
21  	}
22  
23  	/**
24  	 * Generates a number within the given range.
25  	 * 
26  	 * Example: middle = 50; deviation = 10; result will be = [40..60]
27  	 * 
28  	 * @param middle fixed point
29  	 * @param deviation the number can be different from middle point
30  	 * @return  a number max <code>deviation</code> away from <code>middle</middle>
31  	 * @since 0.4
32  	 */
33  	public static int generateWithinRange(final int middle, final int deviation) {
34          final int start = middle - deviation;
35          final int randMax = deviation * 2 + 1;
36          final int randValue = RandomUtil.R.nextInt(randMax);
37          return start + randValue;
38  	}
39  
40  }