這篇文章主要介紹了JavaScript如何實現(xiàn)偽隨機正態(tài)分布,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)專注于企業(yè)營銷型網站、網站重做改版、鎮(zhèn)賚網站定制設計、自適應品牌網站建設、HTML5建站、商城系統(tǒng)網站開發(fā)、集團公司官網建設、成都外貿網站建設、高端網站制作、響應式網頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為鎮(zhèn)賚等各大城市提供網站開發(fā)制作服務。
在游戲開發(fā)中經常遇到隨機獎勵的情況,一般會采取先生成數組,再一個一個取的方式發(fā)隨機獎勵。
下面是js測試正態(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); } } //畫圓點 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; } //畫一個長方形 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++; } } } //權重數組 var wt = [105, 216, 316, 488, 1000, 2000, 3680, 5890];//,14770,71535 //生成結果數組函數,結果為權重數組的索引,從0開始 function generateList() { //生成的結果數組長度 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>
感謝你能夠認真閱讀完這篇文章,希望小編分享的“JavaScript如何實現(xiàn)偽隨機正態(tài)分布”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關知識等著你來學習!
當前標題:JavaScript如何實現(xiàn)偽隨機正態(tài)分布
分享地址:http://jinyejixie.com/article18/gcesdp.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網站改版、移動網站建設、網站導航、網站建設、面包屑導航
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)