简体   繁体   English

如何使用概率并将它们插入我的其他 function?

[英]How do I use probability and insert them into my other function?

//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%);
  }
}

How do I simulateCatch?如何模拟Catch? I dont know how to do and send probabilitties back to catchFish我不知道该怎么做并将概率发回给 catchFish

The idea is to generate a random number between 0 and 1, and then subtract each of the successive probabilities for each of the types of the fish, until the rolled value becomes less than 0. Beware, though, that this assumes the probabilities are in range between 0 and 1 and give 1 in sum.这个想法是生成一个介于 0 和 1 之间的随机数,然后减去每种鱼的每个连续概率,直到滚动值小于 0。但是请注意,这是假设概率在范围在 0 和 1 之间,总和为 1。 Otherwise, it won't work correctly.否则,它将无法正常工作。

 // 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()

You can define the function like this您可以像这样定义 function

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

and update your catchFish like this并像这样更新你的鲶鱼

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);
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将我的JavaScript库公开给其他客户端使用? - How do I expose my javascript library for other clients to use? 我有一个带有许多变量的 function。 如何在另一个 function 中使用它们? - I have a function with many variables. How do I use them in another function? 当其他内容附加到网址片段后,我该如何使用它们? - How do I use url fragments when other things get appended to them? 我如何确保在使用图表之前加载它们? - How do i ensure that i load my charts before i use them? 如何使用我的盒子中的输入到我的 function? - How do I use the input from my box to my function? 我该如何插入 <div> (其中包含我的菜单)进入所有其他html页面? - How do I insert the same <div> (which contains my menu) into all other html pages? 如何使用 Drupal 在另一个页面中调用我的 CSS 或 JavaScript 文件并将它们包含在多个页面中 - How do I use Drupal to call my CSS or JavaScript files in another page and include them in multiple pages 如何使用我的 javascript (const) 中的信息插入我的 html? - How do I use information from my javascript (const) to insert into my html? 如何对调用同一文件/类中的其他函数的函数进行单元测试,以便我无法模拟它们? - How do I unit-test a function that calls other functions in the same file/class so I can't mock them? 如何在代码中正确使用mouseleave函数? - How do I correctly use the mouseleave function in my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM