Random類的總結

 

//介紹Random類,有兩種:java.util.Random或者是java.lang.Math類中的random()方法

import java.util.Random;

public class RandomTest

{

       public static void main(String[] args)

       {   //第一種random,得出的範圍是從0開始到數字的結尾,但不包括最後一個數

              Random random = new Random();

              for(int i = 0; i < 50; i++)

              {

                     //System.out.println(random.nextInt(41) + 10);

             

//第二種random,得出的範圍是0.0到1.0(不包括1.0)

              double result = Math.random();

              result *= 41;

              int result2 = (int)result;

              result2 += 10;

              System.out.println(result2);

              }

       }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章