Results 1 to 7 of 7
Thread: Random Number Generator troubles
 03182013, 05:56 AM #1Member
 Join Date
 Mar 2013
 Posts
 5
 Rep Power
 0
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+" ");
}
}
}
 03182013, 06:50 AM #2Senior Member
 Join Date
 Jun 2007
 Location
 Bali, Indonesia
 Posts
 762
 Rep Power
 9
Re: Random Number Generator troubles
Doesn't it suppose to be like:
Java Code:int r = randomGenerator.nextInt(lowvalue + highvalue);
Website: Learn Java by Examples
 03182013, 08:31 AM #3Member
 Join Date
 Mar 2013
 Posts
 5
 Rep Power
 0
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!
 03182013, 09:19 AM #4Senior Member
 Join Date
 Jun 2007
 Location
 Bali, Indonesia
 Posts
 762
 Rep Power
 9
Re: Random Number Generator troubles
You are right, if you read the doc of the nextInt() method you'll see this statements:
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.Website: Learn Java by Examples
 03182013, 09:40 AM #5Member
 Join Date
 Mar 2013
 Posts
 5
 Rep Power
 0
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 04. But if I use 02 with 30 random numbers, they are all in the range of 02
Ugh.
 03182013, 09:52 AM #6Senior Member
 Join Date
 Jun 2007
 Location
 Bali, Indonesia
 Posts
 762
 Rep Power
 9
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.
Website: Learn Java by Examples
 03182013, 10:42 AM #7Member
 Join Date
 Mar 2013
 Posts
 5
 Rep Power
 0
Similar Threads

Random Phone number generator
By elecleoalune in forum New To JavaReplies: 15Last Post: 05192014, 12:37 PM 
Random number generator
By adjit in forum New To JavaReplies: 9Last Post: 03052012, 05:09 PM 
Help with Random Number Generator
By celtics in forum New To JavaReplies: 0Last Post: 03072011, 09:18 PM 
Random number generator
By zerwik in forum New To JavaReplies: 3Last Post: 12262010, 01:10 PM 
Random number generator
By Michailangelo in forum Advanced JavaReplies: 4Last Post: 04022010, 06:47 PM
Bookmarks