加权随机数选择 [英] Weighted Random number selection

查看:31
本文介绍了加权随机数选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 node.js 应用程序,其中必须根据 [0, 100] % 范围内的获胜概率随机选择获胜者

我的代码如下:

var activeGame = {编号:12324,类型:1,活跃:真实,球员:[{编号:5032,name: "用户名",富:对,获胜概率:56.32//%}, {编号:98243,name: "用户名",富:对,获胜概率:22.68//%}, {编号:10943,name: "用户名",富:假,获胜概率:21.00//%}],};

我发现其他算法不是很清楚,并且在概率加起来达到 100% 时不起作用.

我正在寻找一种方法来创建一个 function selectRandomWinner()返回获胜玩家的索引,但我被卡住了,所有帮助都会不胜感激.谢谢!

解决方案

计算一个从 0 到 100 的随机数.然后循环玩家将他们的概率加到总数中,直到总数大于随机数:

var activeGame = {编号:12324,类型:1,活跃:真实,球员:[{编号:5032,姓名:乔",富:对,获胜概率:56.32//%}, {编号:98243,姓名:简",富:真的,获胜概率:22.68//%}, {编号:10943,姓名:弗雷德",富:假,获胜概率:21.00//%}],};功能pickPlayer(){var randPct = Math.random() * 100;无功总计 = 0;var 玩家 = activeGame.players;var selectedPlayer;for (var i = 0; i 

挑选1000名玩家的结果:<table id="结果"><tr><th>姓名</th><th>Count</th></table>

I'm developing a node.js app where a winner has to be randomly selected based on it's win probability in the range of [0, 100] %

My code is as follows:

var activeGame = {
    id: 12324,
    type: 1,
    active: true,
    players: [{
        id: 5032,
        name: "Username",
        foo: true,
        winProbability: 56.32 //%
    }, {
        id: 98243,
        name: "Username",
        foo: true,
        winProbability: 22.68 //%
    }, {
        id: 10943,
        name: "Username",
        foo: false,
        winProbability: 21.00 //%
    }],
};

I've found other algorithms that weren't very clear and didn't work with probabilities adding up to 100%.

I'm looking for a way to create a function selectRandomWinner() to return the index of the winning player but I'm stuck and all and any help would be greatly appreciated. Thanks!

解决方案

Calculate a random number from 0 to 100. Then loop through the players adding their probability to a total, until the total is higher than the random number:

var activeGame = {
  id: 12324,
  type: 1,
  active: true,
  players: [{
    id: 5032,
    name: "Joe",
    foo: true,
    winProbability: 56.32 //%
  }, {
    id: 98243,
    name: "Jane",
    foo: true,
    winProbability: 22.68 //%
  }, {
    id: 10943,
    name: "Fred",
    foo: false,
    winProbability: 21.00 //%
  }],
};

function pickPlayer() {
  var randPct = Math.random() * 100;
  var total = 0;
  var players = activeGame.players;
  var selectedPlayer;
  for (var i = 0; i < players.length; i++) {
    total += players[i].winProbability;
    if (randPct < total) {
      selectedPlayer = players[i];
      break;
    }
  }
  return selectedPlayer;
}

var results = document.getElementById("results");
var resultObj = {};
for (var i = 0; i < 1000; i++) {
  var playerName = pickPlayer().name;
  if (resultObj[playerName]) {
    resultObj[playerName] ++;
  } else {
    resultObj[playerName] = 1;
  }
}
for (name in resultObj) {
  results.innerHTML += "<tr><td>" + name + "</td><td>" + resultObj[name] + "</td></tr>";
}

Results of picking 1000 players:
<table id="results">
  <tr>
    <th>Name</th>
    <th>Count</th>
</table>

这篇关于加权随机数选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆