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.

Share Improve this question asked Feb 28, 2014 at 15:31 M.K. SafiM.K. Safi 7,03911 gold badges45 silver badges63 bronze badges 1
  • 1 You can do it with Math.random: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – bgusach Commented Feb 28, 2014 at 15:33
Add a ment  | 

8 Answers 8

Reset to default 9

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

本文标签: