java算法之Math.random()隨機概率玩法實例演示
引言
java中的Math.random()是一個在[0,1)范圍等概率返回double數(shù)值類型的算法,基于此函數(shù),我們來延申一些隨機概率算法的變形思路,便于大家對Math.random()函數(shù)的隨機概率理解
1、Math.random()的說明
- Math.random()返回的數(shù)據(jù)范圍是[0,1)
- Math.random()數(shù)據(jù)是等概率返回
- Math.random()返回的數(shù)據(jù)類型是double
- 我們可以通過類型轉(zhuǎn)換來實現(xiàn)整數(shù)型的等概率問題,例如:(int)Math.random()
2、Math.random()的等概率代碼驗證
測試Math.random()函數(shù)的等概率,Math.random()在[0,1)等概率返回double類型的數(shù)據(jù),X出現(xiàn)的次數(shù)除以總測數(shù)來測試是否等概率
代碼演示
public class Random{ public static void main(String[] args) { math(); } /** * @Author wangchengzhi * @Description * 測試Math.random()函數(shù)的等概率 * Math.random()在[0,1)等概率返回double類型的數(shù)據(jù) *出現(xiàn)的次數(shù)除以總測數(shù)來測試是否等概率 * @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)的等概率函數(shù),小于"+p+"出現(xiàn)的概率是"+p); System.out.println((double)count/(double)testTime); } }
3、Math.random()*N的變形
測試Math.random()*N函數(shù)的等概率,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函數(shù)的等概率 * 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)的等概率函數(shù),小于5得數(shù)出現(xiàn)得概率是0.5"); System.out.println((double)count/(double)testTime); } }
4、Math.random()轉(zhuǎn)換對應(yīng)整數(shù)概率的變形
測試Math.random()*N整數(shù)的等概率返回,Math.random()*N等概率返回得范圍為[0,N)(int)(Math.random()*N)等概率返回得范圍為[0,N-1]的整數(shù)出現(xiàn)的概率,這里以N=9為例來測試
代碼演示
public class Random{ public static void main(String [] args){ math2(); } /** * @Author wangchengzhi * @Description * 測試Math.random()*N整數(shù)的等概率返回 *Math.random()*N等概率返回得范圍為[0,N) * (int)(Math.random()*N)等概率返回得范圍為[0,N-1]的整數(shù)出現(xiàn)的概率 * 這里以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("在這次測試中,數(shù)字"+i+"返回的次數(shù)為"+count[i]); } } }
5、Math.random()返回概率轉(zhuǎn)變的變形
隨機返回[0,1)范圍的數(shù)X ,且0~X出現(xiàn)的概率是X的平方 ,x出現(xiàn)的概率是X ,所以Math.max(Math.random(),Math.random())最大值,是兩次都出現(xiàn)x的概率 ,就是x乘以x為x的平方
代碼演示
public class Random{ public static void main(String [] args){ math3(); } /** * @Author wangchengzhi * @Description * 目標: * 隨機返回[0,1)范圍的數(shù)X * 且0~X出現(xiàn)的概率是X的平方 * 分析: * x出現(xiàn)的概率是X * 所以Math.max(Math.random(),Math.random())最大值,是兩次都出現(xiàn)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出現(xiàn)的概率"+(double)count/(double)testTime); System.out.println("k的平方是"+Math.pow(k,2)); } }
結(jié)言:
Math.Random()函數(shù)是一個特殊使用的等概率函數(shù),我們在設(shè)計一些算法的時候,可以巧妙的對該函數(shù)進行變形,以滿足我們的具體業(yè)務(wù)場景,以上的例子希望能對大家拓展思路起到一些引導(dǎo)作用,后面會再給大家分享一些針對該函數(shù)的變形案例。
總結(jié)
到此這篇關(guān)于java算法之Math.random()隨機概率玩法的文章就介紹到這了,更多相關(guān)java Math.random()隨機概率內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Springboot的漫畫網(wǎng)站平臺設(shè)計與實現(xiàn)
本文將基于Springboot實現(xiàn)開發(fā)一個漫畫主題的網(wǎng)站,實現(xiàn)一個比漂亮的動漫連載的網(wǎng)站系統(tǒng),界面設(shè)計優(yōu)雅大方,比較適合做畢業(yè)設(shè)計和課程設(shè)計使用,需要的可以參考一下2022-08-08java加載properties文件的六種方法總結(jié)
這篇文章主要介紹了java加載properties文件的六種方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-05-05Spring-data-redis操作redis cluster的示例代碼
這篇文章主要介紹了Spring-data-redis操作redis cluster的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10SpringBoot項目打包發(fā)布到外部tomcat(出現(xiàn)各種異常的解決)
這篇文章主要介紹了SpringBoot項目打包發(fā)布到外部tomcat(出現(xiàn)各種異常的解決),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09