I would like to generate random numbers between 1 & 10, with a decimal place of 1
(e.g. 1.5, 9.4, 6.3, 2.9)
I would like to generate random numbers between 1 & 10, with 2 decimal places
(e.g. 1.51, 9.46, 6.32, 2.93)
I would like to generate random numbers between 1 & 10, with 3 decimal places
(e.g. 1.512, 9.346, 6.329, 2.935)
One solution would be to have a random generator just for the decimal places and then concatenate with the number. Is there a simpler way to achieve this?
Although BigDecimal solutions are more flexible, here’s a simpler solution using int and double. This method takes a
Randomobject, an upper and lower bound and a number of decimal places and returns an accordingly formatted string:Test Code:
Output: