【问题标题】:How do I use probability and insert them into my other function?如何使用概率并将它们插入到我的其他函数中?
【发布时间】:2021-08-07 02:14:27
【问题描述】:
//Catchfish
function catchFish() {
  if (character === "steve") {
    // STEVE PROBABILITIES: cod (70%), salmon (20%), tropical (5%), puffer (5%)
    simulateCatch (70%, 20%, 5%, 5%);
  
  } else if (character === "alex") {
    // ALEX PROBABILITIES: cod (10%), salmon (10%), tropical (30%), puffer (50%)
    simulateCatch(10%, 10%, 30%, 50%);

  } else if (character === "villager") {
    // VILLAGER PROBABILITIES: cod (25%), salmon (25%), tropical (25%), puffer (25%)
    simulateCatch(25%, 25%, 25%, 25%);
  }
}

如何模拟Catch?我不知道该怎么做并将概率返回给catchFish

【问题讨论】:

  • 你想从simulateCatch函数得到什么输出?您在上面评论过的字符串?
  • @AbdullahRazzaki 是的
  • 只需创建一个有 4 个参数的函数,然后连接一个显示 4 个参数的字符串。我错过了什么吗?你还需要什么?

标签: javascript function probability


【解决方案1】:

我们的想法是生成一个介于 0 和 1 之间的随机数,然后减去每种鱼的每个连续概率,直到滚动值小于 0。但是请注意,这是假设概率在 0 到 1 之间,总和为 1。否则,它将无法正常工作。

// Returns an index of the randomly selected item
function simulateCatch(probabilities)
{
    let roll = Math.random();
  
    for (let i = 0; i < probabilities.length; i++)
    {
      roll -= probabilities[i];
      if (roll <= 0) return i;
    }
    // never gets to this point, assuming the probabilities sum up to 1
}

var fishArray = ["cod", "salmon", "tropical", "puffer"]

//Catchfish
function catchFish(character) {
  if (character === "steve") {
    // STEVE PROBABILITIES: cod (70%), salmon (20%), tropical (5%), puffer (5%)
    return simulateCatch ([0.7, 0.2, 0.05, 0.05]);
  
  } else if (character === "alex") {
    // ALEX PROBABILITIES: cod (10%), salmon (10%), tropical (30%), puffer (50%)
    return simulateCatch([0.1, 0.1, 0.3, 0.5]);

  } else if (character === "villager") {
    // VILLAGER PROBABILITIES: cod (25%), salmon (25%), tropical (25%), puffer (25%)
    return simulateCatch([0.25, 0.25, 0.25, 0.25]);
  }
}

function test()
{
  console.log(fishArray[catchFish("steve")])
  console.log(fishArray[catchFish("alex")])
}

test()

【讨论】:

    【解决方案2】:

    你可以这样定义函数

    function simulateCatch(character,cod,salmon,tropical,puffer){
    return `${character.toUpperCase()} PROBABILITIES: cod (${cod}%), salmon (${salmon}%), tropical (${tropical}%), puffer (${puffer})`;
    }
    
    

    并像这样更新您的 catchFish

    function catchFish() {
      if (character === "steve") {
        // STEVE PROBABILITIES: cod (70%), salmon (20%), tropical (5%), puffer (5%)
        simulateCatch (character,70, 20, 5, 5);
      
      } else if (character === "alex") {
        // ALEX PROBABILITIES: cod (10%), salmon (10%), tropical (30%), puffer (50%)
        simulateCatch(character,10, 10, 30, 50);
    
      } else if (character === "villager") {
        // VILLAGER PROBABILITIES: cod (25%), salmon (25%), tropical (25%), puffer (25%)
        simulateCatch(character,25, 25, 25, 25);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      • 2011-09-05
      相关资源
      最近更新 更多