這篇文章主要介紹了JavaScript如何實(shí)現(xiàn)偽隨機(jī)正態(tài)分布,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
10年積累的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有城西免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。在游戲開發(fā)中經(jīng)常遇到隨機(jī)獎(jiǎng)勵(lì)的情況,一般會(huì)采取先生成數(shù)組,再一個(gè)一個(gè)取的方式發(fā)隨機(jī)獎(jiǎng)勵(lì)。
下面是js測(cè)試正態(tài)分布代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <canvas id="myCanvas" width="800" height="400" > Your browser does not support the canvas element. </canvas> <canvas id="myCanvas2" width="800" height="400" > Your browser does not support the canvas element. </canvas> <textarea id="text" cols="200" rows="5000"></textarea> <script type="text/javascript"> var timesArr = []; var timesArrObj = {}; window.onload = function () { // for (var meter = 0; meter < 800; meter++) { // var times = getNumberInNormalDistribution(20, 7); // addPoint(times, meter); // timesArr.push(Math.floor(times)); // } // drawGreenTab(timesArr,1); // drawLine(0, 380, 800, 380); drawResult(1); drawResult(2); drawResult(0); drawGreenTab(timesArrObj[1], 1); drawGreenTab(timesArrObj[2], 2); drawGreenTab(timesArrObj[0], 0); } //制作綠色柱狀圖表 function drawGreenTab(timesArr, color) { var timesTypes = {}; for (var i in timesArr) { var times = timesArr[i]; if (timesTypes[times] == null) { timesTypes[times] = 0; } else { timesTypes[times] = timesTypes[times] + 1; } } for (var i in timesTypes) { drawRect(i, timesTypes[i], 4, color); } } //畫圓點(diǎn) function addPoint(y, x, color) { y = 400 - y; var c = document.getElementById("myCanvas"); var cxt = c.getContext("2d"); if (color == null) { cxt.fillStyle = "#FF0000"; } else { cxt.fillStyle = color; } cxt.beginPath(); cxt.arc(x, y, 2, 0, Math.PI * 2, true); cxt.closePath(); cxt.fill(); } var meter = 0; //劃線 function drawLine(beginx, beginy, endx, endy) { var c = document.getElementById("myCanvas"); var cxt = c.getContext("2d"); cxt.moveTo(beginx, beginy); cxt.lineTo(endx, endy); cxt.stroke(); } //模擬正態(tài)分布取值 function getNumberInNormalDistribution(mean, std_dev) { return mean + (uniform2NormalDistribution() * std_dev); } //模擬正態(tài)分布偏差 function uniform2NormalDistribution() { var sum = 0.0; for (var i = 0; i < 12; i++) { sum = sum + Math.random(); } return sum - 6; } //畫一個(gè)長(zhǎng)方形 function drawRect(x, y, width, index) { var color = "#FF0000"; if (index == 1) { color = "#00FF00"; } else if (index == 2) { color = "#0000FF"; } var c = document.getElementById("myCanvas2"); var cxt = c.getContext("2d"); cxt.fillStyle = color; cxt.fillRect(x * width + index * 200, 400 - y, width - 2, y); } //畫出生成的圖像 function drawResult(index) { var color = "#FF0000"; if (index % 3 == 1) { color = "#00FF00"; } else if (index % 3 == 2) { color = "#0000FF"; } var result = generateList(); var resultStr = ""; // for (var i in result) { // resultStr = resultStr + result[i] + "\n"; // } //document.getElementById("text").value = resultStr; var resulttimes = {}; for (var i in result) { if (resulttimes[result[i]] == null) { resulttimes[result[i]] = 1; } else { resulttimes[result[i]] = resulttimes[result[i]] + 1; } } for (var i in resulttimes) { resultStr = resultStr + resulttimes[i] + "\n"; } document.getElementById("text").value = resultStr; var timeslist = []; var times = 1; for (var i in result) { if (result[i] == index) { addPoint(times, i / 5, color); if (timesArrObj[index] == null) { timesArrObj[index] = []; } timesArrObj[index].push(times); times = 0; } else { times++; } } } //權(quán)重?cái)?shù)組 var wt = [105, 216, 316, 488, 1000, 2000, 3680, 5890];//,14770,71535 //生成結(jié)果數(shù)組函數(shù),結(jié)果為權(quán)重?cái)?shù)組的索引,從0開始 function generateList() { //生成的結(jié)果數(shù)組長(zhǎng)度 var n = 50000; var wtp = []; var sum = 0; for (var i in wt) { sum = sum + wt[i]; } for (var i in wt) { wtp.push(wt[i] / sum); } var result = []; var p = []; for (var i in wtp) { var inp = getNumberInNormalDistribution(1 / wtp[i], 1 / wtp[i] / 3); p.push(inp); } for (var i = 0; i < n; i++) { var minp = 99999999; var minj = -1; for (var j in p) { if (p[j] < minp) { minp = p[j]; minj = j; } } result.push(minj); for (var j in p) { p[j] = p[j] - minp; } p[minj] = getNumberInNormalDistribution(1 / wtp[minj], 1 / wtp[minj] / 3); } return result; } </script> </body> </html>
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“JavaScript如何實(shí)現(xiàn)偽隨機(jī)正態(tài)分布”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司,關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、網(wǎng)站設(shè)計(jì)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
分享題目:JavaScript如何實(shí)現(xiàn)偽隨機(jī)正態(tài)分布-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)地址:http://aaarwkj.com/article42/dgophc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、微信小程序、域名注冊(cè)、定制開發(fā)、企業(yè)網(wǎng)站制作、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容