![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 65
Rep Power: 3
![]() |
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? |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Sexy Programmer
|
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! |
|
|
|
|
|
#4 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3
![]() |
Quote:
Use Math.random() * (10 - 6) instead. |
|
|
|
|
|
|
#5 |
|
Sexy Programmer
|
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! |
|
|
|
|
|
#6 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3
![]() |
Quote:
![]() 6 + (int) (Math.random() * (10 - 6)) |
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Oct 2005
Posts: 65
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#8 |
|
Sexy Programmer
|
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! |
|
|
|
|
|
#9 | |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#10 |
|
Sexy Programmer
|
it returns 0 - 0.99999999999999999999, better explanation
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|