所选数字的百分比机会JavaScript

时间:2018-08-12 15:13:04

标签: javascript algorithm math

我正在创造一个幸运的纺车。所以我有数字1到6 这是一些代码:

首先我得到一个0到7之间的随机数,所以(1,2,3,4,5,6)

var random = Math.floor(Math.random() * 6) + 1;

我想做的是这样设置百分比:

var goldChance = 10;
var blueChance = 40;
var grayChance = 50;

goldChance是数字25

blueChance是数字63,并且

graychance是数字41

因此,当车轮开始旋转时,对于旋转4,应该显示数字150%,对于{{ 1}},只有稀有数字63应该显示40%的时间。这样,如果幸运的话,您会得到那个黄金数字。

是否可以将Math.random()设置为包含机会?

谢谢!

3 个答案:

答案 0 :(得分:1)

不要得到一个随机数1 <= n <= 6,而是要得到一个随机数0 <= n <= 19,然后从二十个条目的数组中进行选择,并分配数字:

const numbers = [2, 5, 6, 3, 6, 3, 6, 3, 6, 3, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1];
const result = numbers[Math.floor(Math.random() * numbers.length)];

示例:

const numbers = [2, 5, 6, 3, 6, 3, 6, 3, 6, 3, 4, 1, 4, 1, 4, 1, 4, 1, 4, 1];
const tbody = document.getElementById("results");
const rows = [1, 2, 3, 4, 5, 6].map(number => {
  const row = document.createElement("tr");
  row.insertAdjacentHTML("beforeend",
    `<td>${number}</td><td>0</td><td>0%</td>`);
  tbody.appendChild(row);
  return row;
});
let draws = 0;
let timer = 0;
doDraw();

function doDraw() {
  const number = numbers[Math.floor(Math.random() * numbers.length)];
  ++draws;
  const row = rows[number - 1];
  const countCell = row.cells[1];
  countCell.textContent = Number(countCell.textContent) + 1;
  rows.forEach(row => {
    row.cells[2].textContent = (Number(row.cells[1].textContent) / draws * 100).toFixed(2) + "%";
  });
  timer = setTimeout(doDraw, 1);
}

document.getElementById("stop").addEventListener("click", function() {
  clearTimeout(timer);
  timer = 0;
});
body {
  font-family: sans-serif;
}
td {
  text-align: right;
}
The percentages approach the gold (5% each for 2 and 5 [total of 10%]), blue (20% each for 3 and 6 [total of 40%]), and grey (25% each for 1 and 4 [total of 50%]) levels over time as the randomness works itself out...
<table>
  <thead>
    <tr>
      <th>Number</th>
      <th>Occurrences</th>
      <th>%</th>
    </tr>
  </thead>
  <tbody id="results"></tbody>
</table>
<input id="stop" value="Stop" type="button">

答案 1 :(得分:0)

您可以使用具有概率的数组,并根据随机值进行检查和计数。

此函数首先将返回值设置为最后一个可能的索引,并进行迭代,直到随机值的其余部分小于实际概率为止。

概率必须加一。

function getRandomIndexByProbability(probabilities) {
    var r = Math.random(),
        index = probabilities.length - 1;

    probabilities.some(function (probability, i) {
        if (r < probability) {
            index = i;
            return true;
        }
        r -= probability;
    });
    return index;
}

var i,
    probabilities = [0.25, 0.05, 0.2, 0.25, 0.05, 0.2],
    count = {},
    index;

probabilities.forEach(function (_, i) { count[i + 1] = 0; });

for (i = 0; i < 1e6; i++) {
    index = getRandomIndexByProbability(probabilities);
    count[index + 1]++;
}

console.log(count);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

我通过生成数字1 <= n <= 100,然后分配该范围的一部分来表示所需的概率,回答了类似的编码挑战问题。

例如,要获得1〜50%的时间,获得2 10%的时间,依此类推:

if (n > 0 && n <= 50) {
  return 1;
} else if (n > 50 && n <= 60) {
  return 2;
} else if ...