这篇文章主要介绍了基于JavaScript伪随机正态分布代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

在游戏开发中经常遇到随机奖励的情况,一般会采取先生成数组,再一个一个取的方式发随机奖励。

下面是js测试正态分布代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
<canvas id="myCanvas" width="800" height="400" style="border:1px solid #c3c3c3;">
  Your browser does not support the canvas element.
</canvas>
<canvas id="myCanvas2" width="800" height="400" style="border:1px solid #c3c3c3;">
  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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?