Win a copy of The Business Blockchain this week in the Cloud forum!

Math.random()

Kendall Ponder
Ranch Hand
Posts: 205
4

In the code above, doesn't x contain an integer between -1 and 1024? Thanks!

Roel De Nijs
Sheriff
Posts: 10662
144
Kendall Ponder wrote:In the code above, doesn't x contain an integer between -1 and 1024?

No! Your code as-is always returns 0 But even if you would use the adjusted code (notice the parantheses), it's still incorrect.Why? Just keep reading.

From the javadoc of the Math.random() method
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
So after applying the multiplication, you'll get: 0.0 * 1024 <= value < 1.0 * 1024. So it's an int value between 0 (inclusive) and 1024 (exclusive).

Hope it helps!
Kind regards,
Roel

Kendall Ponder
Ranch Hand
Posts: 205
4
Roel De Nijs wrote:
Kendall Ponder wrote:In the code above, doesn't x contain an integer between -1 and 1024?

No! Your code as-is always returns 0 But even if you would use the adjusted code (notice the parantheses), it's still incorrect.Why? Just keep reading.

From the javadoc of the Math.random() method
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
So after applying the multiplication, you'll get: 0.0 * 1024 <= value < 1.0 * 1024. So it's an int value between 0 (inclusive) and 1024 (exclusive).

Hope it helps!
Kind regards,
Roel

I see why I need the parentheses, but isn't an integer between -1 and 1024 the same as and integer between 0 (inclusive) and 1024 (exclusive)? There is no integer between -1 and 0. Also, do you consider saying between 0 and 1024 the same as saying between 0 (inclusive) and 1024 (exclusive)? Thanks!

Roel De Nijs
Sheriff
Posts: 10662
144
Kendall Ponder wrote:but isn't an integer between -1 and 1024 the same as and integer between 0 (inclusive) and 1024 (exclusive)? There is no integer between -1 and 0. Also, do you consider saying between 0 and 1024 the same as saying between 0 (inclusive) and 1024 (exclusive)?

For me, it is not But that might just be me and probably influenced by my understanding of the word "between" (in my native language). That's why I clearly mentioned the "inclusive" and "exclusive" in my previous reply, so it should be crystal clear without any possibility of interpretation.

Just had a glance at the javadoc of the String.substring() method and it uses inclusive/exclusive as well, probably because it's unambiguous and eliminates interpretation by cultural/linguistic differences.

Kendall Ponder
Ranch Hand
Posts: 205
4
I agree with you.

Steve Fahlbusch
Bartender
Posts: 605
7
you will get a value between 0 and1023

Steve Fahlbusch
Bartender
Posts: 605
7
with 0 and 1023 inclusive

Campbell Ritchie
Sheriff
Posts: 51332
86
I suggest that unless you specifically want a double between 0.0 inclusive and 1.0 exclusive, you stop using Math#random. There is a discussion of the ways to get “random” numbers here.

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15627
46
Note that in many places when in Java, if there's something with an interval of numbers, very often the beginning number is inclusive and the ending number is exclusive. For example in String.substring(begin, end) this is the case, and many other places.

Therefore if you say "a number between 0 and 1024" many people will implicitly assume that's 0 inclusive, until 1024 exclusive.

Junilu Lacar
Bartender
Posts: 8743
77
I think it has something to do with the idiom for the traditional for-loop index:

Here, the values taken on by i are in the range 0 (inclusive) to length (exclusive). The idea may be that since programmers are familiar with this idiom, it will be more consistent to use [from..to) when dealing with ranges in other contexts.

Campbell Ritchie
Sheriff
Posts: 51332
86
The indices are designed to correspond to array indices. Those also correspond to the % operator. If you write
myArray[i++ % myArray.length]
you will go through the array from start to finish and then rotate back to the start. When i passes 2147483647, you have problems
A lots of methods are designed to use such 0‑based values; you can see that
myRandom.nextInt(myArray.length)
will correspond exactly to the array indices.

Junilu Lacar
Bartender
Posts: 8743
77
Makes sense. I'm a sucker for symmetry so my lenses tend to be tinted for that kind of thing

Martin Vajsar
Sheriff
Posts: 3752
62
• 1
Junilu Lacar wrote:The idea may be that since programmers are familiar with this idiom, it will be more consistent to use [from..to) when dealing with ranges in other contexts.

This idiom has also the advantage that to - from is equal to the number of elements in the corresponding range.

And somewhat related - using this convention for time intervals (where from and to are instances of date/time) has the great advantage of being independent from the time resolution being used. The other convention (from and to being inclusive) would mean that the entire year 2015 would be encoded as 1/1/2015-12/31/2015 if the resolution was a day, but 1/1/2015 0:00:00-12/31/2015 23:00:00 if the resolution was an hour. Since I have actually encountered such a mess and had to work with it, I can appreciate the inherent simplicity of inclusive - exclusive