Javascript以随机百分比机会执行函数 [英] Javascript execute function by random percentage chances

查看:136
本文介绍了Javascript以随机百分比机会执行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我想按百分比机会触发一个函数

lets say, I would like to trigger a function by percentage chance

function A () { console.log('A triggered'); } //50% chance to trigger

if (Math.random() >= 0.5) A();

现在我想添加更多功能以偶然触发,我做的是

now i would like to add in more function to be trigger by chances, what i did was

//method 1
function B () { console.log('B triggered'); } //10% chance to trigger
function C () { console.log('C triggered'); } //10% chance to trigger

if (Math.random() >= 0.5) {
    A();
} else if (Math.random() > (0.5 + 0.1)) {
    B();
} else if (Math.random() > (0.5 + 0.1 + 0.1)) {
    C();
}

但这使得 A() B()和C()之前获得优先级。因此我将代码更改为

But this make A() get priority before B() and C(). Therefore I change the code into

//method 2
var randNumber = Math.random();

if (randNumber <= 0.5) { A(); }
else if (randNumber > 0.5 && randNumber <= (0.5 + 0.1)) { B(); }
else if (randNumber > (0.5 + 0.1) && randNumber <= (0.5 + 0.1 + 0.1)) { C(); }

此方法看起来 合理 ,但它看起来效率低,因为它需要在每个 if else 函数中都有硬编码的机会,如果我有一个很长的函数和触发机会的列表,我会需要制作 if else 非常漫长而且凌乱

This method looks fair but it looks inefficient because it need to be hardcoded chances in every single if else function and if I have a list long of function and triggering chances, I will need to make the if else very long and messy

问题是我有什么方法可以做得更好,有效?与上面显示的这两种方法不同。

Question is is there any method I can done this better and efficient? unlike these 2 method i shown above.

* 公平和正方形 也很重要(听起来像游戏)

*also fair and square is important as well (sound like a game)

很抱歉,如果我严重解释情况,我们将不胜感激。谢谢。

Sorry if I explain the situation badly, Any help to this will be appreciated. Thanks.

推荐答案

您可以创建要调用的函数列表以及调用它们的机会。然后使用机会将随机数的范围划分为块。在这个例子中,它将是:

You could create a list of the functions you want to call and their chance to be called. Then you divided the range of the random numbers into chunks using the chances. In this example it would be:

0.0         0.5           0.6           0.7
     0.5/A         0.1/B         0.1/C

不需要根据机会对条目进行排序。如果机会的总和大于1.0,则不会调用数组的最后元素。这是你需要用这种方法确保自己的东西。

The entries do not need to be sorted based on the chances. If the sum of the chances is larger then 1.0 then the last elements of the array won't be called. That's something you need to ensure yourself with this approach.

代码可能如下所示:

function A() {
  console.log('A was called')
}

function B() {
  console.log('B was called')
}

function C() {
  console.log('C was called')
}


var list = [
  {chance: 0.5, func: A},
  {chance: 0.1, func: B},
  {chance: 0.1, func: C}
];

function callRandomFunction(list) {
  var rand = Math.random() // get a random number between 0 and 1
  var accumulatedChance = 0 // used to figure out the current

  var found = list.find(function(element) { // iterate through all elements 
    accumulatedChance += element.chance // accumulate the chances
    return accumulatedChance >= rand // tests if the element is in the range and if yes this item is stored in 'found'
  })

  if( found ) {
    console.log('match found for: ' + rand)
    found.func()
  } else {
    console.log('no match found for: ' + rand)
  }
}

callRandomFunction(list)

这篇关于Javascript以随机百分比机会执行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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