Being able in Java to generate a random number, from an arbitrary number, to another arbitrary number (from lower bound to upper bound). Note that the lower bound is included and the upper bound is excluded.
Example: If the lower bound is 2 and upper bound is 15, the code below will generate a random number from 2 to 14 inclusive.
NOTE: Math.random( ) generates a random number bigger than zero and smaller than one – therefore a double. This is why we must multiply it to get an integer value and then discard the fractional part using (int)
int myRandom = (int)((Math.random() * (lower - upper) + upper));
Example:
int myRandom = (int)((Math.random() * (2 - 15) + 15));
This program generates 20 random numbers to ensure that both the upper bound and the lower bound feature in the solution.