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

Thread: Very simple question (nextDouble- Random class)

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Very simple question (nextDouble- Random class)

    *
    If I change the method to nextInt, it works fine though. How can I use nextDouble without getting the error message? Method next.Double cannot be applied. Required : no arguments, Found: int. Keep in mind, I need the values of the randoms to be in the range 10-20.


     
    import java.util.Random;
     
     
    class Randoms
    {
        public static void main (String[] args)
        {
            Random r = new Random();
     
     
            for (int x = 0; x < 10000; x++)
            {
     
            double random = r.nextDouble(10) + 10;           // This line is not working. *
            System.out.println(random);
            }
        }
     
    }


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Very simple question (nextDouble- Random class)

    There is no param to nextDouble(). It returns a number between 0.0 and 1.0.

    One way of getting it to do it with 10 is to multiply the result of nextDouble by 10 and put it in a double variable.

  3. The Following User Says Thank You to GoodbyeWorld For This Useful Post:

    TSSF44 (October 27th, 2013)

  4. #3
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Very simple question (nextDouble- Random class)

    Quote Originally Posted by GoodbyeWorld View Post
    There is no param to nextDouble(). It returns a number between 0.0 and 1.0.

    One way of getting it to do it with 10 is to multiply the result of nextDouble by 10 and put it in a double variable.
    Thank you. That's what I thought. My professor insists that I use nextDouble. By doing what you said, I can get the range from 10 to 20? It seems to be 0 - 10.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Very simple question (nextDouble- Random class)

    Then add 10 to that result. C'mon, think!

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    TSSF44 (October 27th, 2013)

  7. #5
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Very simple question (nextDouble- Random class)

    Quote Originally Posted by GregBrannon View Post
    Then add 10 to that result. C'mon, think!
    Thanks, I figured it out after posting that. I shouldn't be so quick to ask questions.

Similar Threads

  1. I have tried making a simple random calculator and its not working?
    By StackOfCookies in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2012, 04:00 PM
  2. [SOLVED] (Simple) How to use an if statement to respond to random integers
    By DusteroftheCentury in forum Loops & Control Statements
    Replies: 9
    Last Post: January 27th, 2012, 09:15 PM
  3. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  4. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM