您好,登錄后才能下訂單哦!
這篇文章主要介紹了JavaScript如何實現偽隨機正態分布,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
在游戲開發中經常遇到隨機獎勵的情況,一般會采取先生成數組,再一個一個取的方式發隨機獎勵。
下面是js測試正態分布代碼:
<!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(); } //模擬正態分布取值 function getNumberInNormalDistribution(mean, std_dev) { return mean + (uniform2NormalDistribution() * std_dev); } //模擬正態分布偏差 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如何實現偽隨機正態分布”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。