Math.random()
returns value between 0.0
(including) and 1.0
(excluding) say it returns 0.05013371..
(for example) than your method will do the following operation,
0.05013371.. * 100000 = 5013.371...
(int) 5013.371... = 5013
5013 % 1000 = 13 // Incorrect
But other way around you can still use Math.random()
in a different way to solve this,
int upperBound = 999;
int lowerBound = 100;
int number = lowerBound + (int)(Math.random() * ((upperBound - lowerBound) + 1));
Explanation,
100 + (int)((Number >= 0.0 and < 1.0) * (999 - 100)) + 1;
100 + (int)((Number >= 0.0 and < 1.0) * (899)) + 1;
MIN This can return,
100 + (int)(0.0 * (899)) + 1;
100 + 0 + 1
101
MAX This can return,
100 + (int)(0.9999.. * (899)) + 1;
100 + 898 + 1
999
NOTE : You can change upper and lower bound accordingly to get required results.