Results 1 to 7 of 7
  1. #1
    sara12345 is offline Member
    Join Date
    Dec 2009
    Posts
    59
    Rep Power
    0

    Default generating double random number between 0 and 1

    hello
    can somebody tell me how to generate double random number between 0 and 1
    I have used the following but it gives me an error
    random.nextDouble(1.0)

    thanks

  2. #2
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Java Code:
    import java.util.Random;
    
    Random r = new Random();
    double zeroToOne = r.nextInt(1001) / 1000

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Please don't open duplicate threads. To answer your question, the most simple way would be
    Java Code:
    double d = Math.random();

  4. #4
    sara12345 is offline Member
    Join Date
    Dec 2009
    Posts
    59
    Rep Power
    0

    Default

    sorry I didn't duplicate it may be there is an error
    I found that random.nextDouble() it self gives random number between 0 and 1
    thanks alot

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Cruncher View Post
    Java Code:
    import java.util.Random;
    
    Random r = new Random();
    double zeroToOne = r.nextInt(1001) / 1000
    Bad boy! Bad, bad, bad boy! No treats for you!

    But seriously Cruncher, please test your advice before giving it if possible. For example here's my test of your recommendation:
    Java Code:
    import java.util.Random;
    
    public class RandomFo {
      public static void main(String[] args) {
        Random r = new Random();
        
        for (int i = 0; i < 40; i++) {
          double zeroToOne = r.nextInt(1001) / 1000;
          System.out.println(zeroToOne);
        }
      }
    }
    Run this and tell me what you see.


    (to the OP, please ignore Cruncher's reply and go with Moon's).
    Last edited by Fubarable; 04-17-2010 at 08:00 PM.

  6. #6
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    sorry divide it by 1000.0 then it will recognize as a double. for got that java says int/int = int

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Cruncher View Post
    sorry divide it by 1000.0 then it will recognize as a double. for got that java says int/int = int
    Even that's a bad idea as it results in a "grainier" randomization (and thus less random) than is possible with either Moon's recommendation or the OP's final choice. It's much better to go with the methods already available in the API for creating random doubles than what you've suggested, even with the modification.

Similar Threads

  1. problems generating random objects
    By mandangalo in forum New To Java
    Replies: 8
    Last Post: 07-30-2010, 04:42 AM
  2. generating random number between 0 and 1
    By sara12345 in forum New To Java
    Replies: 1
    Last Post: 04-17-2010, 07:54 PM
  3. Powerball Example (Generating Random Numbers)
    By a7x75 in forum New To Java
    Replies: 8
    Last Post: 02-12-2010, 02:38 PM
  4. Generating a random number
    By oridov in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 05:12 PM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 AM

Posting Permissions

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