-4

I am trying to round to the nearest decimal value, however, this line of code keeps returning a number between 0 and 1, I also want the output to be between 1 and 10. Where am I going wrong?

power[i] = rng.nextDouble();
  • 1
    I @Anton, please post more code; in this way we can understand better your problem – A. Wolf Oct 18 '18 at 18:00
  • 2
    Please read the documentation of the `nextDouble` method. It probably states that it returns a random number between `0` (inclusive) and `1` (exclusive). Why do you expect something different? Please do some research before posting a question on StackOverflow, this is an actual requirement (see [ask]). There are many questions such as [How to generate random integers within a specific range in Java?](https://stackoverflow.com/questions/363681/how-to-generate-random-integers-within-a-specific-range-in-java) – Zabuzard Oct 18 '18 at 18:00
  • 1
    I think [Generate a random double in range](https://stackoverflow.com/a/3680648/4417306) is just what you need. – Mark Oct 18 '18 at 18:00

2 Answers2

-1

You can use JDK's Math.round.

A detailed example can be found here.

Yuri
  • 3,283
  • 1
  • 19
  • 37
  • This answer is more suited as **comment**. Please do not abuse the answering system for incomplete answers. See [answer], thanks. – Zabuzard Oct 18 '18 at 18:01
-1

Math.round(ThreadLocalRandom.nextDouble(1,10)*10)/10.0

LunaticJape
  • 852
  • 9
  • 29