Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: My random generator isn't working. Please help!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My random generator isn't working. Please help!

    The random number should be between number1 and number2, an inclusive but after the first random number request, it goes outside my stipulation.
    import java.util.*;
    public class RandomNumber{
        public static void main(String[] args)
        {
            Random variable = new Random();
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter  number 1: ");
            int number1 = sc.nextInt();
            System.out.println("Enter  number 2: ");
            int number2 = sc.nextInt();
     
            if(number1>number2)
            {
                int a = (variable.nextInt((number1-number2) + number2));
                System.out.println(a);
            }
            else
            {
                int a = (variable.nextInt((number2-number1) + number1));
                System.out.println(a);
            }
        }
    }
    Last edited by helloworld922; November 4th, 2011 at 11:03 PM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: My random generator isn't working. Please help!

    First, please highlight your code with [highlight=Java] *Code* [/highlight]

    Secondly, your naming is terrible, the variable name should give you an idea of what the variable is.

    Thirdly, you need to be more specific about your problem. See:
    Be Explicit
    and
    Posting Code

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My random generator isn't working. Please help!

    THIS IS NOT THE MOST EFFICIENT WAY TO DO THIS. BUT IT IS THE EASIEST IN MY OPINION:

    Okay consider this:
    Start with random being equal to 0;
    While your random number is smaller than the smallest number OR your random number is bigger than the biggest number you need to keep retrying
    Create a random number up to the largest number and let that be random's value.
    Once random meets your qualifications the loop will stop.
    Then outside your while loop, the variable random is a number that fits your qualifications.

    If you need any further help please ask!

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: My random generator isn't working. Please help!

    At skaterboy987, that code is not just inefficient but also not as simple as it could be. (You can get the number in one line of code, no loops)

    At tripline, thank you for highlighting your code; now for generating a random number, you only have a certain amount of randomness. EG if you were trying to find a number between 2 and 4 inclusive, you would only have a range of 3 possible different numbers (2, 3 and 4). Now, how could you use that to generate a number between the first number and the second number?
    Last edited by Tjstretch; November 4th, 2011 at 11:16 PM.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My random generator isn't working. Please help!

    skaterboy - you are saying do a while loop until the random numbers hit between number1 and number2? I guess I can try that.
    Tjstretch - What line of code are you talking about? I did a fair amount of research and tried to do it as simple as possible as well.
    (variable.nextInt((2) +2 ) ?
    Starting at 2, I can only go up by 2?

  6. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: My random generator isn't working. Please help!

    Yes, that is the basics of the idea, but since you are having trouble I will try to help you a little bit more:
    int smallestNumber = 2; //This could be anything
    int largestNumber = 4;  //Same
    int range = largestNumber - smallestNumber; //What is the range of randomness in the numbers?
    Technically, I suppose this is two lines if you include range as one of lines of code.
    Now, with those numbers, how could you devise a Random statement that would go between the smallest number and the largest number? Hint, you will have to use addition outside of the nextInt method.

    Note that in order to be inclusive, you will have to add one in your gaining of the random number. (This is intuitively obvious when you see the rest of the line of code and note that nextInt(n) is between 0 (inclusive) and n (exclusive))

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My random generator isn't working. Please help!

    Well I feel like an idiot. My parentheses were in the wrong place. I had to rewrite the entire thing to figure it out. Really wish someone would have said syntax error.

    import java.util.*;
    public class RandomNumber{
        public static void main(String[] args)
        {
            Random variable = new Random();
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter  number 1: ");
            int number1 = sc.nextInt();
            System.out.println("Enter  number 2: ");
            int number2 = sc.nextInt();
            int range1 = (number1 - number2);
            int range2 = (number2 - number1);
     
            if(number1>number2)
            {
                int a = (variable.nextInt(range1)) + number2;
                System.out.println(a);
            }
            else
            {
                int a = (variable.nextInt(range2)) + number1;
                System.out.println(a);
            }
        }
    }

  8. #8
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: My random generator isn't working. Please help!

    There was no syntax error, you were using different logic. The code you posted was completely valid syntactically

    EDIT: Does that give you an inclusive random number?
    Last edited by Tjstretch; November 5th, 2011 at 12:54 AM.

  9. #9
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My random generator isn't working. Please help!

    yes.

    My original ((number1-number2) + number2)) //parentheses in the wrong place

  10. #10
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: My random generator isn't working. Please help!

    That has no compile times errors, that is valid syntactically, it just doesn't do what you were trying to do. How am I supposed to know you knew the logic and just could not convert it into Java if you did not say what you thought the logic was going to be?

Similar Threads

  1. Random Number Generator Always gives 0
    By tlckl3m3elmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2012, 03:09 PM
  2. HELP random generator with roll() and print() method
    By disc_dido in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 18th, 2011, 01:50 PM
  3. random number generator
    By java3 in forum Loops & Control Statements
    Replies: 4
    Last Post: February 21st, 2011, 12:00 PM
  4. Replies: 0
    Last Post: January 25th, 2011, 01:24 AM
  5. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM