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