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 4 of 4

Thread: Generating a random number from a range of inputted numbers

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Generating a random number from a range of inputted numbers

    Hello,

    I am trying to randomly generate a number from a range of numbers.

        public static void defineLucky(){
            System.out.println("\nChoose a lucky number from a range "
                    + "of numbers \n");
            System.out.println("Enter lo number: ");
            int lo = scanner.nextInt();
            System.out.println("Enter hi number: ");
            int hi = scanner.nextInt();
            System.out.println("");
            System.out.println("Your lucky number is: " + getLucky(lo,hi));  
        }
        public static int getLucky(int lo, int hi){
            int total = 0;
            for (int i = lo; i <= hi; ++i){
                total += i;            
            }
            return total;
        }

    Example: Right now when I put 5 and 10 in it is giving me 45 and I'm not really sure what needs done to fix this. I'm wanting it to give me a random number between 5 and 10.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Generating a random number from a range of inputted numbers

    That's exactly what I would expect it to give you. 5+6+7+8+9+10 = 45. Why would this code give you a random number of any kind?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Generating a random number from a range of inputted numbers

    It's not and that's my problem. I've been trying to figure out how to get it to work without any luck. That was just my latest attempt, and only thing that wasn't giving me errors.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Generating a random number from a range of inputted numbers

    Take a look at the Math.random() function or the Random class.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Generating random numbers in java without repeating a specific number
    By ojonugwa in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 26th, 2013, 12:42 AM
  2. Replies: 5
    Last Post: November 29th, 2012, 01:25 PM
  3. Generating a Random Coprime Number
    By phleep in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 15th, 2011, 12:12 AM
  4. Generating random numbers
    By abelned in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2010, 07:24 AM
  5. Generating random letters and numbers together
    By newJava in forum Java Theory & Questions
    Replies: 3
    Last Post: March 19th, 2010, 04:08 AM