• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vishal Hegde
Ranch Hand
Posts: 1077
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
 
shukla raghav
Ranch Hand
Posts: 203
  • 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
 
Adomin Hoghog
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
 
Misha van Tol
Ranch Hand
Posts: 56
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its greater than or equal to 0.0 and less than 1.0.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic