• Post Reply Bookmark Topic Watch Topic
  • New Topic
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().....any formula to adjust required range?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HELLO ALL
I am looking for a formula or method which takes our required minimum & maximum range and will return numbers within our specified range using Math.random().Possible signature of such method is like this,

Is this thing is presented by some author.?
Best Regards,
Hassan!
[ December 22, 2002: Message edited by: Hassan Marzooq ]
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Hassan,
i really don't see a problem you are facing. do you want to know how you can generate random numbers between the number range you provide?
if thats the case then try the following code,

modify this to your needs...
if this was not the problem then please clarify more to us...
regards
maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
sorry in creating constructor in main i swapped two arguments
it should be random(min,max) ..
regards
maulin
 
Hassan Marzooq
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HELLO.
Exactly for the same code i was looking for.But i am amazed that why any author didn't mention the same technique.Anyway, thanks for sharing code.
Best Regards,
Hassan.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i found this code on the web.
and i think this is also a greate Random class

hope this will help
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that the code given by Maulin will never return the specified maximum value, because the number returned by Math.random() will always be less than 1, and int arithmetic rounds down for positive numbers. E.g. if min is 1 and max is 6, the largest value you might see is 5.999999... - but when you round it to an int, it's 5, not 6.
The intRandom(low, high) method shown by Joyce is correct - it has an extra +1 which makes the necessary difference.
[ December 23, 2002: Message edited by: Jim Yingst ]
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- Peter
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
of course i meant it to be the way it is. it won't return MAX number anytime. i just shifted the range from [0.0,1.0) to [min,max)
once basics works then grooming doesn't take much time
thanks for pointing that out.
regards
maulin
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic