Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 调用函数的概率百分比_Javascript_Jquery - Fatal编程技术网

Javascript 调用函数的概率百分比

Javascript 调用函数的概率百分比,javascript,jquery,Javascript,Jquery,所以,我有一个按钮,可以激活功能一,功能二或功能三。但是我希望函数1有60%的几率通过按钮被调用,函数2和函数3有20%的几率被调用。 谢谢你的帮助 代码= 函数一{alert'one';} 函数二{alert'two';} 函数三{alert'three';} 考虑以下代码: function activateRandom () { var random = Math.random(); // get a random number from 0 to 1 if (rando

所以,我有一个按钮,可以激活功能一,功能二或功能三。但是我希望函数1有60%的几率通过按钮被调用,函数2和函数3有20%的几率被调用。 谢谢你的帮助

代码=

函数一{alert'one';} 函数二{alert'two';} 函数三{alert'three';}
考虑以下代码:

function activateRandom () {
    var random = Math.random(); // get a random number from 0 to 1
    if (random < 0.6) { // 60% chance to get below 0.6
        one();
    } else if (random < 0.8) { // 20% chance to go between 0.6 and 0.8
        two();
    } else { // 20% chance remaining
        three();
    }
}

用这个按钮,做这个

<button type = "button">' put name of button here' </button>
var rand = Math.random(0, 100)

if(rand < 'put your random percentage here') {
'what function?'
}
对于随机百分比,请执行此操作

<button type = "button">' put name of button here' </button>
var rand = Math.random(0, 100)

if(rand < 'put your random percentage here') {
'what function?'
}

尝试在每个函数中使用它。

到目前为止您做了什么?你熟悉Math.random吗?不,我到目前为止从未使用过Math.random。可能重复的@anotheruser不包括问题正文中的答案。如果你想问一个新的问题,那么就不要把它改成一个新的问题。考虑在投票下点击复选标记,这样人们就知道问题已经被回答了。我怎么能像我有四个函数那样修改它呢?基本上只是扩展代码。好的,非常感谢你!我会点击复选标记,当我能做到这一点!