• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Math.random().....what is the starting range ad ending range?can we customise the range numbers ?

 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whats the starting and ending range when we use Math.random() can we customise the range numbers
 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Math.random() randomly generates values of type double between 0.0000 to 1.000 including the boundary values. now using this you can generate all randome values by using an appropriate multiplier

like

5.0 * Math.random() would generate random values between 0.00 to 5.00
300 * Math.random() would generate random values between 0.00 to 300.00

its very simple, i dont know why would you need to customise its range
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Between 0.0 and 1.0. Hint:you can multiply+cast the result to get a wellformed int.
Java 6 API docs
 
Ranch Hand
Posts: 56
Android Mac Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its greater than or equal to 0.0 and less than 1.0.
 
Marshal
Posts: 64696
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of you is correct. If you had looked in the API you would have known the correct answer.
 
lowercase baba
Posts: 12751
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can multiply and add to get just about any range you want. if you need something between 10-20, that's the same as getting a value from 0-10 and adding 10 to it. So, you multiply by the RANGE of values you need, then add (or subtract) an offset (if needed. You can then round to an int, if that is needed.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!