Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 12th, 2006, 5:14 AM   #1
kurt
Programmer
 
Join Date: Oct 2005
Posts: 71
Rep Power: 4 kurt is on a distinguished road
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?
kurt is online now   Reply With Quote
Old Feb 12th, 2006, 5:32 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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.
Arevos is offline   Reply With Quote
Old Feb 12th, 2006, 12:07 PM   #3
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
highest = 10
lowest = 6
6 + (int) (Math.random() *10)

__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Feb 12th, 2006, 12:27 PM   #4
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
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.
Polyphemus_ is offline   Reply With Quote
Old Feb 12th, 2006, 1:02 PM   #5
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
yeaaaah! Sorry it only works when the lowest number is 1, sorry!

6 + (int) (Math.random() * 10 - 6)
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Feb 12th, 2006, 1:20 PM   #6
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
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))
Polyphemus_ is offline   Reply With Quote
Old Feb 12th, 2006, 1:28 PM   #7
kurt
Programmer
 
Join Date: Oct 2005
Posts: 71
Rep Power: 4 kurt is on a distinguished road
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.
kurt is online now   Reply With Quote
Old Feb 12th, 2006, 1:51 PM   #8
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
put (int) in front of the statement, it will turn out with no demicals!
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Feb 12th, 2006, 3:56 PM   #9
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
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
jaeusm is offline   Reply With Quote
Old Feb 12th, 2006, 4:05 PM   #10
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
it returns 0 - 0.99999999999999999999, better explanation
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:54 PM.

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