Random Number Generator troubles

I have an assignment to prompt a user for a low value (must be 0 or 1), a high value (any number higher than 1), and the total number of numbers they want to be generated between the two values. The portion of my textbook which describes the Random object doesn't seem to include all the information I need to accomplish this. This is the code I came up with, which obviously doesn't work. I know that the RNG will have to be located inside a loop that repeats until the total amount of numbers the user asks for is reached, but for now I'm just concentrating on getting a number between the two values entered by the user. Thanks for any advice/help.

package forloopsrandom;

import java.util.*;

public class Forloopsrandom2 {

public static void main (String[] args) {

Scanner keyboard = new Scanner(System.in);

Random randomGenerator = new Random();

System.out.println("This program will generate random numbers. ");

System.out.println("Please enter the lowest value possible (0 or 1): ");

int lowvalue = keyboard.nextInt();

if (lowvalue >= 0 && lowvalue <= 1) {

System.out.print("Enter the highest value possible. ");

int highvalue = keyboard.nextInt();

System.out.print("How many numbers would you like?");

int totalnumbers = keyboard.nextInt();

int r = randomGenerator.nextInt(lowvalue) + highvalue;

System.out.print(r+" ");

}

}

}

Re: Random Number Generator troubles

Doesn't it suppose to be like:

Code:

`int r = randomGenerator.nextInt(lowvalue + highvalue);`

The nextInt() will throw exception when you are not passing a positive integer.

Re: Random Number Generator troubles

Your advice was partially correct. However, when I run the program and I input 0 as the minimum value and 2 as the maximum value, the results all come back as either 0 or 1. To fix this, I used the following line instead:

int randomInt = randomGenerator.nextInt(lowvalue+(highvalue+1));

Thank you for the help!

Re: Random Number Generator troubles

You are right, if you read the doc of the nextInt() method you'll see this statements:

Quote:

Returns a pseudorandom, uniformly distributed int value **between 0 (inclusive)** and **the specified value (exclusive)**, drawn from this random number generator's sequence. The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned.

Re: Random Number Generator troubles

Well, I thought I had it correct. For some reason if I enter a min of 0 I get the correct range for my output. However, if I put 1 for the min it will return a range from 0 to max+1. For example, if I put in a minimum of 1 and a max of 3, and I ask for 30 random numbers between them, I will get a range of 0-4. But if I use 0-2 with 30 random numbers, they are all in the range of 0-2

Ugh.

Re: Random Number Generator troubles

If you read again the doc you'll understand. The nextInt() method doesn't know your low and high value. In only accept one value of n. And will generate random value between 0 inclusive and the specified number (n) exclusive. So if you give (1 + 3 + 1) which is 5, you'll get 0..4. And if you give (0 + 2 + 1) which is 3, it will give you 0..2.

Re: Random Number Generator troubles

Thank you for your help. I found a nice way to keep the numbers inside the range no matter what the input:

int randomInt = randomGenerator.nextInt(highvalue - lowvalue + 1) + lowvalue;

Thanks again, I appreciate it!