Posts

Showing posts with the label ember-cli-code-coverage

How do I generate random integers within a specific range in Java?

Image
How do I generate a random  int  value in a specific range? The following methods have bugs related to integer overflow: randomNum = minimum + ( int )(Math.random() * maximum); // Bug: `randomNum` can be bigger than `maximum`. Random rn = new Random (); int n = maximum - minimum + 1 ; int i = rn.nextInt() % n; randomNum = minimum + i; // Bug: `randomNum` can be smaller than `minimum`. java random integer Share Improve this question Follow edited  Oct 4 at 10:08 Ramesh R 6,805 4 4 gold badges 24 24 silver badges 37 37 bronze badges asked  Dec 12, 2008 at 18:20 user42155 47.8k 27 27 gold badges 58 58 silver badges 60 60 bronze badges When you need a lot of random numbers, I do not recommend the Random class in the API. It has just a too small period. Try the  Mersenne twister  instead. There is  a Java implementation .   –  raupach   Dec 13, 2008 at 10:18   35 Before you post a new answer, consider there are already 65+ a...