So I am confused as to how to generate random integers between a range, in the first one it says to make the range from 0 to n-1 and the precondition is that n has to be greater than 0. For the second
import java.util.Random ;
/**
* This class represents a variety of methods that generate random integer values
*
* @author
* @version
*/
public class RandomIntegers
{
private static Random random = new Random();
/** returns a random integer from 0 to n - 1
* @param n the upper limit (exclusive)
* Precondition: n > 0
* @return a random integer from 0 to n - 1
*/
public static int randomInteger(int n)
{
int rando = random.nextInt(n-1);
return rando ;
}
/** returns a random integer from start to end (inclusive)
* @param start the lower limit of random numbers (inclusive)
* @param end the upper limit of random numbers (inclusive)
* Precondition: 0 <= start <= end
* @return a random integer from start to end (inclusive)
*/
public static int randomInteger(int start, int end)
{
return random.nextInt((end - start) + 1) + start;
}