random.nextInt方法用法


 

1、不帶參數的nextInt()會生成所有有效的整數(包含正數,負數,0)

2、帶參的nextInt(int x)則會生成一個范圍在0~x(不包含X)內的任意正整數

  例如:int x=new Random.nextInt(100); 

    則x為一個0~99的任意整數

3、生成一個指定范圍內的整數

 /*
     * 生成[min, max]之間的隨機整數
     * @param min 最小整數
     * @param max 最大整數
     */
    private static int randomInt(int min, int max){
        return new Random().nextInt(max)%(max-min+1) + min;
    }

 


例如:調用方法randomInt(10,20);則會生成一個[10,20]集合內的任意整數

random.nextInt()的用法

標簽:max   int()   static   rand   dom   隨機   ext   exti   and   

原文地址:https://www.cnblogs.com/hm1990hpu/p/8875928.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM