Results 1 to 7 of 7
  1. #1
    cupofdoug is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default 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+" ");

    }
    }
    }

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Random Number Generator troubles

    Doesn't it suppose to be like:

    Java Code:
    int r = randomGenerator.nextInt(lowvalue + highvalue);
    The nextInt() will throw exception when you are not passing a positive integer.

  3. #3
    cupofdoug is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default 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!

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default 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.

  5. #5
    cupofdoug is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default 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.

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default 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.

  7. #7
    cupofdoug is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default 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!

Similar Threads

  1. Random Phone number generator
    By elecleoalune in forum New To Java
    Replies: 15
    Last Post: 05-19-2014, 01:37 PM
  2. Random number generator
    By adjit in forum New To Java
    Replies: 9
    Last Post: 03-05-2012, 05:09 PM
  3. Help with Random Number Generator
    By celtics in forum New To Java
    Replies: 0
    Last Post: 03-07-2011, 09:18 PM
  4. Random number generator
    By zerwik in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 01:10 PM
  5. Random number generator
    By Michailangelo in forum Advanced Java
    Replies: 4
    Last Post: 04-02-2010, 07:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •