Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Random double (inclusive of range) (http://www.programmingforums.org/showthread.php?t=8374)

kurt Feb 12th, 2006 4:14 AM

Random double (inclusive of range)
 
I'm trying to get a random double number with includes the low and high bound itself.

I've tried:

:

Math.random()*(highBound()-lowBound())+lowBound());

if i enter say 6 to 10 for low and high bound respectively, i can't get the number 6.0000 and 10.0000.

Is it possible to include both ends?

Arevos Feb 12th, 2006 4:32 AM

Math.random should return a number that is greater than or equal to 0, and less than 1. So your lower bound at least, will eventually turn up.

The problem is the 'eventually' part. Doubles are 64 bit types, so there's 2^64 representable values between 1 and 0; that's 1.8e19 in standard form. With this many choices, it'll be a while before you end up with exactly zero, especially considering that there are only 2.4e9 seconds in the average western human lifespan.

It's perfectly possible to include both ends, but statistically you won't see them unless you limit the number of values that Math.random can return. Perhaps you only need integers, or perhaps you only need numbers that go down to two decimal places.

ReggaetonKing Feb 12th, 2006 11:07 AM

highest = 10
lowest = 6
6 + (int) (Math.random() *10)

;)

Polyphemus_ Feb 12th, 2006 11:27 AM

Quote:

Originally Posted by reggaeton_king
highest = 10
lowest = 6
6 + (int) (Math.random() *10)

;)

Won't work, when Math.random() returns 1, you will get the number 16.

Use Math.random() * (10 - 6) instead.

ReggaetonKing Feb 12th, 2006 12:02 PM

yeaaaah! Sorry it only works when the lowest number is 1, sorry!

6 + (int) (Math.random() * 10 - 6)

Polyphemus_ Feb 12th, 2006 12:20 PM

Quote:

Originally Posted by reggaeton_king
yeaaaah! Sorry it only works when the lowest number is 1, sorry!

6 + (int) (Math.random() * 10 - 6)

Still wrong ;)

:

6 + (int) (Math.random() * (10 - 6))

kurt Feb 12th, 2006 12:28 PM

Yeah, thanks guys.

For getting integers from 6 to 10, i have no problems.

It's decimal points from 6.00 to 10.00 that i can't get the both ends.

Since Arevos said that the numbers will eventually turn up, i guess there's no problem then.

I asked this because i thought that math.random returns a value between 0 to 1, not including 0 and 1. Now i'm confused.

ReggaetonKing Feb 12th, 2006 12:51 PM

put (int) in front of the statement, it will turn out with no demicals!

jaeusm Feb 12th, 2006 2:56 PM

Quote:

I asked this because i thought that math.random returns a value between 0 to 1, not including 0 and 1. Now i'm confused.
Math.random() returns a double greater than or equal to 0.0 and less than 1.0

ReggaetonKing Feb 12th, 2006 3:05 PM

it returns 0 - 0.99999999999999999999, better explanation


All times are GMT -5. The time now is 9:14 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC