0

How can I generate a random double in Java?

All tutorials only show how to generate in a certain range, but I want all doubles possible.

Is there something which just shuffles all bits and gives me the number?

DNA
  • 42,007
  • 12
  • 107
  • 146
wutzebaer
  • 14,365
  • 19
  • 99
  • 170
  • 1
    How about a random double within the range of possible values for a double? – tnw May 05 '15 at 19:34
  • no i dont want a range, please read – wutzebaer May 05 '15 at 19:34
  • 2
    I did read. Your random double would be within the range of possible values of double. – tnw May 05 '15 at 19:35
  • please read, i dont want a range, i want one of any possible double – wutzebaer May 05 '15 at 19:35
  • 3
    You can just set the range to be the range of min and max double value... – Dyrborg May 05 '15 at 19:35
  • 2
    All random numbers require a range.... You might be able to generate a random number between 0 and the Max size of a Double on your machine though – Mo H. May 05 '15 at 19:35
  • isn't it possible to just shuffle the 64bits of an double? – wutzebaer May 05 '15 at 19:39
  • 1
    Don't know how I can make this any clearer. You do realize that "any possible double" means a RANGE of values right? Why are you trying to re-invent the wheel? Just use the built in random functions. – tnw May 05 '15 at 19:39
  • 1
    @wutzebaer shuffling thbits randomly could give you NaN's, which you almost certainly don't want – vandale May 05 '15 at 19:41
  • Your computer can't store numbers up to infinity with primitives. int, double, float, etc. all have a max range, and that range is different from computer to computer. So you can only work within that range. – Mo H. May 05 '15 at 19:42
  • NaNs are ok it's security test – wutzebaer May 05 '15 at 19:44

2 Answers2

7

What you can do is generate a random 64-bit value. This can give you Not-A-Number and +/-Infinity.

Random rand = new Random();

double d = Double.longBitsToDouble(rand.nextLong());

Note: Random only produces 2^48 possible long values. You can use SecureRandom to generate all possible 64-bit values but this is much slower.

In terms of real numbers, your rand is [-Double.MAX_VALUE, Double.MAX_VALUE] The distribution is even for all possible representations.

You have about 1/4096 chance of having a NaN. You could loop this operation to discard any undesirable values.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    @wutzebaer No, we didn't. We suggested you go a different and much easier direction than you were (seemingly) pointlessly demanding. – tnw May 05 '15 at 19:44
  • 1
    @wutzebaer I think they were implying it didn't make mathematical sense. ;) – Peter Lawrey May 05 '15 at 19:44
  • may be but i need this as a security test – wutzebaer May 05 '15 at 19:45
  • 1
    @wutzebaer and you could have done it ways more easily had you actually tried to listen to what we were suggesting, while this answer works (and I like it alot tbh) it is way more complicated of a solution then it needs to be. You could just as easily have used the built in random provided by java – Mo H. May 05 '15 at 19:47
  • but the build in version just gives numbers in the range 0-1 – wutzebaer May 05 '15 at 19:48
  • Which you can turn into a range of 0-MAX_DOUBLE, in the end though its your solution, if this is what you feel comfortable with, so be it. – Mo H. May 05 '15 at 19:50
  • but i need negative numbers too – wutzebaer May 05 '15 at 19:50
  • @wutzebaer Did you even Google something like `java generate random double in range` which is what practically everybody suggested? One of the first results: http://stackoverflow.com/questions/9723765/generating-a-random-double-number-of-a-certain-range-in-java You are making it *extremely* difficult to help you. – tnw May 05 '15 at 19:51
  • @wutzebaer you could also generate it so that it is in the negative range, I just chose 0 as the end point. – Mo H. May 05 '15 at 19:51
0

Numbers are big. If there theoretically existed a way to generate a random number from among "all" numbers, there's a 99.99999....% chance that the number would be larger that anything representable in memory, even if you had every atom in the universe as memory.

You have to choose a range, period.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55