I hadn't been here for fifteen years
I hadn't been here for fifteen years
I hadn't been here for fifteen years
Stephan van Hulst wrote:If that degree of precision is necessary, then double is the wrong data type in the first place.
I learned in Calculus that 0.999~ = 1.0. This means that if you have infinite precision, your range will be inclusive.
This sounds like differential calculus where you need to consider the greatest possible (tends toward 1) value as == one...
I hadn't been here for fifteen years
I hadn't been here for fifteen years
A double can represent exactly any integer which will fit into the 53 bits' precision available in the IEEE754 format. So all values in the range of an int can be converted to a double safely, but the larger values in the range of a long mostly cannot be represented as doubles. So that should work nicely.A.J. Côté wrote: . . . System.out.println( (double)Integer.MAX_VALUE1 == Integer.MAX_VALUE1); . . .
I hadn't been here for fifteen years
Sorry for the confusion. Because you have −1 on both sides, it cancels itself out. It was the division of MAX_VALUE by itself that I was using. Not possibilities − 1, which you were correct about.(double)Integer.MAX_VALUE1
Simon Roberts wrote:
Maybe you can take Math.random() * 2 and if it's > 1, throw it away and retry.
Ted Schrey wrote:I need to have inclusive for use in a probability determination. Specifically the probability that something exists. if 0.0, definitely does not exist. anything between 0.0 and 1.0, it might exist to the degree of probability and if 1.0 it definitely exists. I know I can't use Math.random() since it excludes 1.0.
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
Sometimes the only way things ever got fixed is because people became uncomfortable.
Winston Gutkowski wrote:This will return an integer between 0 and 100 inclusive, with a good probability that both 0 and 100 are as likely to appear as any other value.
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
… and the sign bit at the extreme left of the number.Tim Holloway wrote:. . .
A floatingpoint number in binary form consists of . . . the exponent and the mantissa.
. . .
Campbell Ritchie wrote:
… and the sign bit at the extreme left of the number.Tim Holloway wrote:. . .
A floatingpoint number in binary form consists of . . . the exponent and the mantissa.
. . .
Sometimes the only way things ever got fixed is because people became uncomfortable.
Tim Holloway wrote:I'm a little out of practice on floatingpoint terminology, so I may get some of the terms scrambled, but I think I need to outline what's really happening here and how it shapes the options.
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
Sometimes the only way things ever got fixed is because people became uncomfortable.
Stephan van Hulst wrote:
Then you'd have a double value in the range [0,10), which doesn't even have a uniform distribution.
[edit]
I posted this as Guillermo edited his post.
I'm not sure what adding this difference would achieve, besides messing with the probability distribution.
Guillermo Ishi wrote:Adding the difference or 0 randomly would allow you to have 0.. 1 inclusive. I don't think it would mess with the distribution except that it would reduce the probability of getting 0 unless you fixed that.
Also, you might be able to multiply it by some number to simply scale the range .000000001 (or whatever) amount.
Guillermo Ishi wrote:What would you have if you read the number, and then randomly you added 0 or the difference between your highest number and 1 to the result?
Sometimes the only way things ever got fixed is because people became uncomfortable.
Ted Schrey wrote:I need to have inclusive for use in a probability determination. Specifically the probability that something exists. if 0.0, definitely does not exist. anything between 0.0 and 1.0, it might exist to the degree of probability and if 1.0 it definitely exists. I know I can't use Math.random() since it excludes 1.0.
Stephan van Hulst wrote:Geez Louise, rounding would achieve exactly the same. I think it's clear: Simon's way is the way to go. It's correct, intuitive, and the probability distribution is uniform.
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
Henry Wong wrote:
Simon Roberts wrote:
Maybe you can take Math.random() * 2 and if it's > 1, throw it away and retry.
Agree with Simon. I would also go for the bigger range and discard technique ... although, I would likely choose a smaller bigger range, and hence, have a lower chance of discard.
Henry
I hadn't been here for fifteen years
Winston Gutkowski wrote:I disagree. Sorry to keep harping on this, but a simple rounding of the value, whether multiplied by some "granularity" factor or not, does not solve the problem, for reasons already mentioned by Tim  namely, that the extremes (0.0 and 1.0) would be only half as likely to appear as any other value.
Winston Gutkowski wrote:
So why not just use nextInt() and divide? Providing the upper limit (ie, your "granularity") is reasonable, it ensures that the extremes OP wants are built in to the algorithm from the start, and don't rely on any fancy rounding to achieve them.
Winston
Sometimes the only way things ever got fixed is because people became uncomfortable.
Tim Halloran wrote:Be careful here it is easy to get this wrong ... One draft of Effective Java (second edition) went into great depth on the issues of constructing a correct nextInt(int n) method for Random, Josh presented, formally, how and why dividing went wrong (with regard to the distribution) but I think that got mostly cut from the book at publication.
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
Simon Roberts wrote:Maybe you can take Math.random() * 2 and if it's > 1, throw it away and retry...
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Fair enough, and I see your point; but one has to assume that the writers of Random were competent enough for it to produce a decent statistical spread of numbers.
Sometimes the only way things ever got fixed is because people became uncomfortable.
Tim Holloway wrote:And that, precisely, is the problem. The random function isn't picking numbers like you pick playing cards from a deck. It's picking numbers like you throw dice. Meaning that there is a finite and calculable probability that you'll do the equivalent of throwing "snake eyes" 32 times in a row.
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
I'm not quite sure where the discussion about coins comes into play.
Sometimes the only way things ever got fixed is because people became uncomfortable.
I brought this back from the farm where they grow the tiny ads:
Garden Master Course kickstarter
https://coderanch.com/t/754577/GardenMasterkickstarter
