Friday, February 27, 2015

Java: Random


In this example we create a random speed jump by a certain constant.

private Random random = new Random();

private static final double SPEED_M_PER_S = 2.2352;  /* 5 MPH */
private static final double MAX_RANDOM_DELTA = 0.13;

double speed = SPEED_M_PER_S 
+ Math.min(MAX_RANDOM_DELTA, random.nextDouble()) 
* (random.nextBoolean() ? -1 : 1);