Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

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

Vishal Hegde
Ranch Hand
Posts: 1077
whats the starting and ending range when we use Math.random() can we customise the range numbers

shukla raghav
Ranch Hand
Posts: 203
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
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
Its greater than or equal to 0.0 and less than 1.0.

Campbell Ritchie
Sheriff
Posts: 50749
83
One of you is correct. If you had looked in the API you would have known the correct answer.

fred rosenberger
lowercase baba
Bartender
Posts: 12234
36
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.