programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Math.random()

Ranch Hand
Posts: 205
4
• Number of slices to send:
Optional 'thank-you' note:

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

Sheriff
Posts: 11604
178
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:

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: 11604
178
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
I agree with you.

Bartender
Posts: 612
7
• Number of slices to send:
Optional 'thank-you' note:
you will get a value between 0 and1023

Steve Fahlbusch
Bartender
Posts: 612
7
• Number of slices to send:
Optional 'thank-you' note:
with 0 and 1023 inclusive

Marshal
Posts: 79406
377
• Number of slices to send:
Optional 'thank-you' note:
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.

Java Cowboy
Posts: 16084
88
• Number of slices to send:
Optional 'thank-you' note:
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.

Sheriff
Posts: 17652
300
• Number of slices to send:
Optional 'thank-you' note:
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
Marshal
Posts: 79406
377
• Number of slices to send:
Optional 'thank-you' note:
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
Sheriff
Posts: 17652
300
• Number of slices to send:
Optional 'thank-you' note:
Makes sense. I'm a sucker for symmetry so my lenses tend to be tinted for that kind of thing

Sheriff
Posts: 3837
66
• 1
• Number of slices to send:
Optional 'thank-you' note:

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