java算法之Math.random()隨機概率玩法實例演示
引言
java中的Math.random()是一個在[0,1)范圍等概率返回double數值類型的算法,基于此函數,我們來延申一些隨機概率算法的變形思路,便于大家對Math.random()函數的隨機概率理解
1、Math.random()的說明
- Math.random()返回的數據范圍是[0,1)
- Math.random()數據是等概率返回
- Math.random()返回的數據類型是double
- 我們可以通過類型轉換來實現整數型的等概率問題,例如:(int)Math.random()
2、Math.random()的等概率代碼驗證
測試Math.random()函數的等概率,Math.random()在[0,1)等概率返回double類型的數據,X出現的次數除以總測數來測試是否等概率
代碼演示
public class Random{ public static void main(String[] args) { math(); } /** * @Author wangchengzhi * @Description * 測試Math.random()函數的等概率 * Math.random()在[0,1)等概率返回double類型的數據 *出現的次數除以總測數來測試是否等概率 * @Date 14:31 2022/12/16 * @Param * @return **/ public static void math(){ int testTime=100000000; int count =0; double= 0.5; for(int i=0;i<testTime;i++){ if(Math.random()<p){ count++; } } System.out.println("Math.random()為[0,1)的等概率函數,小于"+p+"出現的概率是"+p); System.out.println((double)count/(double)testTime); } }
3、Math.random()*N的變形
測試Math.random()*N函數的等概率,Math.random()得等概率范圍為[0,1),Math.random()*N等概率返回得范圍為[0,N),這里以N=10為例來測試.
代碼演示
public class Random{ public static void main(String[] args) { math1(); } /** * @Author wangchengzhi * @Description * 測試Math.random()*N函數的等概率 * Math.random()得等概率范圍為[0,1) *Math.random()*N等概率返回得范圍為[0,N) * 這里以N=10為例來測試 * @Date 14:31 2022/12/16 * @Param * @return **/ public static void math1(){ int testTime=100000000; int count =0; int p=10; for(int i=0;i<testTime;i++){ if(Math.random()*p<5){ count++; } } System.out.println("Math.random()*10為[0,10)的等概率函數,小于5得數出現得概率是0.5"); System.out.println((double)count/(double)testTime); } }
4、Math.random()轉換對應整數概率的變形
測試Math.random()*N整數的等概率返回,Math.random()*N等概率返回得范圍為[0,N)(int)(Math.random()*N)等概率返回得范圍為[0,N-1]的整數出現的概率,這里以N=9為例來測試
代碼演示
public class Random{ public static void main(String [] args){ math2(); } /** * @Author wangchengzhi * @Description * 測試Math.random()*N整數的等概率返回 *Math.random()*N等概率返回得范圍為[0,N) * (int)(Math.random()*N)等概率返回得范圍為[0,N-1]的整數出現的概率 * 這里以N=9為例來測試 * @Date 14:31 2022/12/16 * @Param * @return **/ public static void math2(){ int testTime=100000000; int m=9; int [] count = new int[m]; for(int i=0;i<testTime;i++){ int res=(int)(Math.random()*m); count[res]++; } for(int i=0;i<m;i++){ System.out.println("在這次測試中,數字"+i+"返回的次數為"+count[i]); } } }
5、Math.random()返回概率轉變的變形
隨機返回[0,1)范圍的數X ,且0~X出現的概率是X的平方 ,x出現的概率是X ,所以Math.max(Math.random(),Math.random())最大值,是兩次都出現x的概率 ,就是x乘以x為x的平方
代碼演示
public class Random{ public static void main(String [] args){ math3(); } /** * @Author wangchengzhi * @Description * 目標: * 隨機返回[0,1)范圍的數X * 且0~X出現的概率是X的平方 * 分析: * x出現的概率是X * 所以Math.max(Math.random(),Math.random())最大值,是兩次都出現x的概率 * 就是x乘以x,x的平方 * @Date 14:31 2022/12/16 * @Param * @return **/ public static void math3(){ int testTime=100000000; int count =0; double k =0.8; for(int i=0;i<testTime;i++){ if(Math.max(Math.random(),Math.random())<k){ count++; } } System.out.println("k出現的概率"+(double)count/(double)testTime); System.out.println("k的平方是"+Math.pow(k,2)); } }
結言:
Math.Random()函數是一個特殊使用的等概率函數,我們在設計一些算法的時候,可以巧妙的對該函數進行變形,以滿足我們的具體業(yè)務場景,以上的例子希望能對大家拓展思路起到一些引導作用,后面會再給大家分享一些針對該函數的變形案例。
總結
到此這篇關于java算法之Math.random()隨機概率玩法的文章就介紹到這了,更多相關java Math.random()隨機概率內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring-data-redis操作redis cluster的示例代碼
這篇文章主要介紹了Spring-data-redis操作redis cluster的示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10SpringBoot項目打包發(fā)布到外部tomcat(出現各種異常的解決)
這篇文章主要介紹了SpringBoot項目打包發(fā)布到外部tomcat(出現各種異常的解決),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09