Generating Random Doubles in Java [duplicate]
问题 This question already has answers here : Generate a random double in a range (5 answers) Closed 6 years ago . I'm a Java noob trying to generate a random double between -10 and 10 inclusive. I know with ints I would do the following: Random r = new Random(); int i = -10 + r.nextInt(21); However, with doubles, this doesn't work: Random r = new Random(); double i = -10 + r.nextDouble(21); Can someone please explain what to do in the case of doubles? 回答1: Try this: Random r = new Random();