Java随机函数
Java有三種獲取隨機數的方法
一. java.util.Random包下的Random類,通過new一個Random對象來產生隨機數。
二. Math.random()方法,產生的隨機數是[0,1)之間的一個double數。
三.?System.currentTimeMillis(),該方法返回從1970年1月1號0點0分0秒到現在的毫秒數,返回值為long。
Random類
Random類有兩種構造方法:帶種子和不帶種子。
不帶種子:
每次運行結果不一樣。默認種子為System.currentTimeMillis(),即系統時間作為seed。因為時間一直在改變所以種子也在變。
Random() rdm = new Random();?
//Random() rdm = new Random(System.currentTimeMillis());
帶種子:
每次運行結果都一樣。相同種子的運行結果一樣。
Random rdm = new Random(100);
Random類中的8種方法
rdm.nextInt() //返回int取值范圍內的偽隨機數,[2147483648 , 2147483647]
rdm.nextInt(n) //返回0~n的偽隨機數,[0 , n)
rdm.nextLong()//返回long取值范圍內的偽隨機數,[-(2^63-1) , 2^63]
rdm.nextDouble() //返回double類型的偽隨機數,[0.0 , 1.0)
rdm.nextFloat() //返回float類型的偽隨機數,[0.0 , 1.0)
rdm.nextBytes(bytes)//用隨機數填充指定字節數組的元素
rdm.nextBoolean()//返回true或false
rdm.nextGaussian()//返回高斯/正態分布
接下來對以上方法進行測試
import java.util.Random;public class TestRandom2 {public static void main(String[] args) {Random rdm1 = new Random();//不帶種子Random rdm2 = new Random(100);//帶種子Random rdm3 = new Random(100);//與rdm2帶一樣的種子byte[] data = new byte[3];//用隨機數填充data數組rdm1.nextBytes(data);for(int i=0;i<3;i++){System.out.println("不帶種子的nextInt(): "+rdm1.nextInt());}System.out.println();for(int i=0;i<3;i++){System.out.println("不帶種子的nextInt(10): "+rdm1.nextInt(10)+",");}System.out.println();for(int i=0;i<3;i++){System.out.println("rdm2帶種子數100: "+rdm2.nextInt(10));}System.out.println();for(int i=0;i<3;i++){System.out.println("rdm3帶種子數100: "+rdm3.nextInt(10));}System.out.println();System.out.println("nextLong(): "+rdm1.nextLong());System.out.println("nextDouble(): "+rdm1.nextDouble());System.out.println("nextFloat(): "+rdm1.nextFloat());System.out.println("nextGaussian(): "+rdm1.nextGaussian());System.out.println("nextBoolean(): "+rdm1.nextBoolean());System.out.print("nextBytes(bytes): ");for(int i=0;i<3;i++){System.out.print(data[i]+" ");}} }
第二次運行結果
可以看出帶種子的構造方法,兩次運行結果是一樣的。種子數相同運行結果也是一樣的。
不帶種子的兩次運行結果則不同。
Math.random()
產生[0,1)之間的double類型隨機數Math.random()*n>>>[0,n)之間的隨機數 (int)(Math.random()*n)>>>[0,n)之間的隨機整數 ?//int后面的()不能缺,否則會先強制轉換Math.random()
Math.round(Math.random()*(MAX-MIN)+MIN)>>>[MIN,MAX]//Math.round()可以暫時理解為四舍五入函數
進行測試 double temp1;int temp2;long temp3;System.out.print("[0,1): ");for(int i=0;i<3;i++){temp1=Math.random();System.out.print(temp1+" ");}System.out.println();System.out.print("[0,9]: ");for(int i=0;i<3;i++){temp2=(int) (Math.random()*10);System.out.print(temp2+" ");}System.out.println();System.out.print("[0,10]: ");for(int i=0;i<3;i++){//Math.round 四舍五入函數(負數的話小數等于5時不進位,-1.5=-1)temp3=Math.round(Math.random()*9+1);System.out.print(temp3+" ");}
運行結果
System.currentTimeMillis()
返回當前的系統時間,返回值為longtemp=System.currentTimeMillis()%100; //對系統時間取模,返回[0,99]之間的隨機整數
此方法有個很大的缺陷,當兩次取值的時間間隔非常小時,取出的值是相同的。 因此我們需要在循環中加入sleep。
進行測試 long r1,r2;System.out.print("返回當前毫秒數: ");for(int i=0;i<3;i++){r1 = System.currentTimeMillis();System.out.print(r1+" ");}System.out.println();System.out.print("加入sleep(): ");for(int i=0;i<3;i++){r1=System.currentTimeMillis();System.out.print(r1+" ");try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println();//對系統時間進行模運算System.out.print("一百以內隨機數: ");for(int i=0;i<3;i++){r2=System.currentTimeMillis()%100;System.out.print(r2+" ");}System.out.println();//sleep后的模運算System.out.print("加入sleep(): ");for(int i=0;i<3;i++){r2=System.currentTimeMillis()%100;System.out.print(r2+" ");try {Thread.sleep(70);} catch (InterruptedException e) {e.printStackTrace();}}
運行結果
總結
- 上一篇: 灵性图书馆:好书推荐-《当下的力量》
- 下一篇: C++连连看外挂