封裝一個方法:
import java.util.Random; public class RandomUtil { /** * nextInt(num) 產生[0 ~ (num-1)]的隨機數, 閉區間 * @param Min * @param Max * @return */ public static int getRandomInt(int Min , int Max){ Random rand = new Random(); return rand.nextInt(Max - Min + 1) + Min; } }
