13 Replies Latest reply on Jul 21, 2009 8:30 AM by YoungWinston

    random number ....

    801443
      I want to generate a random number of type double in the range of 0 to 1.25 but the current function in java that is nextDouble() returns a double only in the range 0.0 to 1.0. How can i generate a random number beyond this range?
        • 1. Re: random number ....
          3004
          Multiply the results by1.25.
          • 2. Re: random number ....
            843789
            As a rough hack I was going to suggest the same.
            • 3. Re: random number ....
              EJP
              ;-)

              Rough compared to what?
              • 4. Re: random number ....
                801443
                Just on a side note i also figured out that the number 1.0d is exclusive in the random number result. But i want that to be included else it would generate random numbers exclusive of 1.0d and hence some of the cases in my application might not be touched at all.
                • 5. Re: random number ....
                  843789
                  ejp wrote:
                  ;-)

                  Rough compared to what?
                  I'm no math genius and I thought that maybe someone who is, would be able to provide a better solution.
                  • 6. Re: random number ....
                    EJP
                    Well it seems perfect to me, and I have a B.A. (Math).

                    @OP: multiply by 1.250001, or however many zeros you need.
                    • 7. Re: random number ....
                      843789
                      roaan wrote:
                      Just on a side note i also figured out that the number 1.0d is exclusive in the random number result. But i want that to be included else it would generate random numbers exclusive of 1.0d and hence some of the cases in my application might not be touched at all.
                      If you are doing something like:
                      if(randomNumber == 1.0) {
                          ....
                      }
                      //etc
                      then I suggest using a different approach. Due to how floating point numbers are implemented you may found that condition never be true. Maybe you could generate an int in a comparable range instead.
                      • 8. Re: random number ....
                        843789
                        ejp wrote:
                        Well it seems perfect to me, and I have a B.A. (Math).

                        @OP: multiply by 1.250001, or however many zeros you need.
                        OK. I was just concerned that a number like 0.00000000000000000000000000000000001 would never be generated.
                        • 9. Re: random number ....
                          EJP
                          Fair enough but I would guess the OP only needs two decimal places ...
                          • 10. Re: random number ....
                            801443
                            >
                            If you are doing something like:

                            if(randomNumber == 1.0) {
                            ....
                            }
                            //etc


                            then I suggest using a different approach. Due to how floating point numbers are implemented you may found that condition never be true. Maybe you could generate an int in a comparable range instead.
                            >

                            I am not following this method of comparison but i have different intervals which would be called if the random number is at extreme case of 1.25 so i think what the above post suggested of multiplying it by 1.250000001 should work out in this scenario as well.
                            • 11. Re: random number ....
                              796365
                              Note that if you generate random doubles you will create numbers like this:

                              0.6787759359406641
                              0.9071493283285994
                              0.06185697703958071
                              0.49053292966982065
                              0.9962640326621838
                              0.10773559293402624

                              Also, Java's standard methods exclude the upper limit value of one [1.] from the generated values, as you noted.

                              If full decimal doubles like above are what's wanted, and you can exclude the lower limit of zero from the generated values, subtract the generated value from one. This will result in a range that excludes zero and includes one.

                              If you want, say, 2-digit decimals from 0 (inclusive) to 1.25 (inclusive), then do as flounder suggested and use ints. Using Random.nextInt(126) divided by 100 and rounded to 2 decimal places should produce that result.

                              Alternatively, there are 3rd-party libs that are inclusive of both end points of a random double range. Se, for example,
                              [https://uncommons-maths.dev.java.net/nonav/api/org/uncommons/maths/random/Probability.html]
                              • 12. Re: random number ....
                                801443
                                @all

                                Thanks!!!!!!!!! I have this resolved now.
                                • 13. Re: random number ....
                                  YoungWinston
                                  roaan wrote:
                                  I am not following this method of comparison but i have different intervals which would be called if the random number is at extreme case of 1.25 so i think what the above post suggested of multiplying it by 1.250000001 should work out in this scenario as well.
                                  Be careful you're not skewing the result with your multiplier, especially if you're rounding as well.

                                  Winston