Results 1 to 7 of 7
Thread: Random Number Generator troubles
- 03-18-2013, 04:56 AM #1
Member
- 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+" ");
}
}
}
- 03-18-2013, 05:50 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Random Number Generator troubles
Doesn't it suppose to be like:
The nextInt() will throw exception when you are not passing a positive integer.Java Code:int r = randomGenerator.nextInt(lowvalue + highvalue);
Website: Learn Java by Examples
- 03-18-2013, 07:31 AM #3
Member
- 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!
- 03-18-2013, 08:19 AM #4
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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
- 03-18-2013, 08:40 AM #5
Member
- 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 0-4. But if I use 0-2 with 30 random numbers, they are all in the range of 0-2
Ugh.
- 03-18-2013, 08:52 AM #6
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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
- 03-18-2013, 09:42 AM #7
Member
- Join Date
- Mar 2013
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Random number generator
By adjit in forum New To JavaReplies: 9Last Post: 03-05-2012, 04:09 PM -
Random Phone number generator
By elecleoalune in forum New To JavaReplies: 13Last Post: 04-20-2011, 09:47 AM -
Help with Random Number Generator
By celtics in forum New To JavaReplies: 0Last Post: 03-07-2011, 08:18 PM -
Random number generator
By zerwik in forum New To JavaReplies: 3Last Post: 12-26-2010, 12:10 PM -
Random number generator
By Michailangelo in forum Advanced JavaReplies: 4Last Post: 04-02-2010, 06:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks