admin管理员组文章数量:1279150
I'd like to make a function that returns true
most of the time when it's called but that will return false
occasionally?
By occasionally I mean, at 1000 calls, it'll have an average of one false
every 50 calls.
I'd like to make a function that returns true
most of the time when it's called but that will return false
occasionally?
By occasionally I mean, at 1000 calls, it'll have an average of one false
every 50 calls.
- 1 You can do it with Math.random: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – bgusach Commented Feb 28, 2014 at 15:33
8 Answers
Reset to default 9Pretty easy, use Math.random
to get a number between 0 and 1, and pare it to 0.02
or the rate of true/false you are looking for.
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.
So, basically:
var randomizer = function () {
return Math.random() > 0.02;
};
EDIT
If you want to make it more beautiful, you can pack this function in an object, and set there the ratio of true/false.
var randomizer = {
_ratio: 0.02, // default ratio
setRatio: function (falses, total) {
this._ratio = falses / total;
},
getResult: function () {
return Math.random() > this._ratio;
}
};
and then
randomizer.getResult();
randomizer.setRatio(60, 1000); // 60 out of 10000
function mostlyFalse() {
return Math.random() <= 0.05;
}
jsfiddle randomly showing mostly 'true's, but with the occasional 'false': http://jsfiddle/t8E6t/1/
How about
Odds: 50/1000 = 1/20
//Get random number from 1 to 20, if equals 20 return false
if (Math.floor((Math.random()*20)+1) == 20)
{
return false;
}
else{
return true;
}
With Math.random
, you can achieve what you want :
function suchRandom(chanceInPercent){
return Math.random() > chanceInPercent/100;
}
console.log(suchRandom(2)); //because 1000/50 = 20 true / 1000 calls = 2%
Around 20 : http://jsfiddle/Ru7qY/1/
function foo () { return Math.random() > 0.02; }
// 1 / 50 == 0.02, so if it's less than 0.02 return false
Create a random number each time the function runs, between 1 and 50:
var num = Math.floor((Math.random()*50)+1);
Then, if the number equals 50, for example, return false.
I feel like the most concise, simple answer is this one-liner function
// A function that has a 50% chance of returning true by default
function chance( c = 0.5 ){
return Math.random() < c;
}
Use it like so
// Block has a 60% chance of being executed.
if( chance( .6 ) ){
//Do stuff
}
// Or, to solve your problem specifically
var c = chance( 49 / 50 );
counter.... :D
var c = 0;
function f()
{
return ++c % 50 == 0 ? false : true;
}
本文标签:
版权声明:本文标题:How to make a JavaScript function that usually returns true, but returns false randomly, once every 50 times on average? - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741217176a2360271.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论