JDK1.7及以上
int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1); // 包含max,所以要加1
JDK1.7以前
// Random的实例化需要考虑,最好能够保持单实例 int randomNum = new Random().nextInt((max - min) + 1) + min;
参考:
https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java