Here are two of the variations to generate random integers within a range (start, end)
:
method 1:
int rand = (int)(Math.random()*(end-start)+1)+start;
method 2:
Random numbergenerator = new Random();
int rand=numbergenerator.nextInt(end-start)+start;
Method 2 seems to work for me.
The reason I ask this: I was trying to debug my code and when I changed my random int
generation to method 2, everything seem to work fine. Method 1 was giving unexpected results.
The unexpected results come from different post: but that is a distraction to this question still it is here. Here I replaced mehtod 1 with method 2 and everything worked fine.
so the question is are these methods of random int generation different. method 1 I used has been upvoted in this post.
What would be an example case where they both could differ (if they are different)?