45

I only know how I can generate a random boolean value (true/false). The default probability is 50:50

But how can I generate a true false value with my own probability? Let's say it returns true with a probability of 40:60 or 20:80 etc...

2
  • 3
    Generate a random number between 1 and 100 and compare it to your distribution?
    – David
    Aug 12, 2014 at 23:46
  • What you did so far?
    – Lior Elrom
    Aug 12, 2014 at 23:59

7 Answers 7

70

One way is checking that the return value of Random.Next(100) is less than your desired probability. I can't speak to the true 'randomness' of this method though.

Proper example, using desired probability of 20%:

Random gen = new Random();
int prob = gen.Next(100);
return prob < 20;
3
  • 1
    I would dare to assume that the predictability of these boolean values is just as random as Random.Next() would be. I don't know if that is the same as it's randomness though.
    – oɔɯǝɹ
    Jul 27, 2016 at 20:00
  • 1
    For me, in ASP.Net the working syntax turned out a little more elaborate: myRandomBool = new Random().Next(100) <= 50 ? true : false
    – GerardV
    Jul 2, 2019 at 19:32
  • 5
    @E. Moffat: ? true : false is redundant code.
    – Alain
    Mar 4, 2021 at 12:14
22

You generate a random number up to 100 exclusive and see if it's less than a given percent. Example:

if(random.Next(100) < 40) {
  // will be true 40% of the time
}

More generally, for a probability of X/Y, use an idiom like:

if(random.Next(Y) < X)
19

Here is an extension method that will provide a random bool with specified probability (in percentage) of being true;

public static bool NextBool(this Random r, int truePercentage = 50)
{
    return r.NextDouble() < truePercentage / 100.0;
}

you can use this like

Random r = new Random();
r.NextBool(); // returns true or false with equal probability
r.NextBool(20); // 20% chance to be true;
r.NextBool(100); // always return true
r.NextBool(0); // always return false
8

Assuming your probability is represented as double between 0.0 and 1.0, I would implement it more simply like this:

Random rand = new Random();
...
double trueProbability = 0.2;
bool result = rand.NextDouble() < trueProbability;

result will be true with the probability given by trueProbability

http://msdn.microsoft.com/en-us/library/system.random.nextdouble(v=vs.110).aspx

If this isn't "random enough", you can take a look at RNGCryptoServiceProvider:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider(v=vs.110).aspx

3
  • to be fully representative of the trueProbability shouldn't you do a <= instead of < test? Jun 23, 2015 at 22:51
  • 1
    @hubsonbropa No, since NextDouble() returns a value between 0 (inclusive) and 1 (exclusive). If you extrapolate to 100% probability, < 1 is sufficient.
    – Mike
    Jun 23, 2015 at 22:57
  • 1
    @hubsonbropa Also, 0% should never be true. The only way to cover that case is to check for < 0.
    – Mike
    Jun 23, 2015 at 23:31
7

I think it can help you

Random gen = new Random();
bool result = gen.Next(100) < 50 ? true : false;
1
  • 10
    The expression gen.Next(100) < 50 is boolean expression. No need to use the conditional operator here. Jul 9, 2015 at 4:23
2

For future knowledge:

40:60 would be:

var random = new Random();
return random.Next(10) < 4;

20:80 would be:

var random = new Random();
return random.Next(5) == 0

and 1:1 would be:

var random = new Random();
return random.Next(2) == 1;

Note: Just shorten the probability to the shortest variant - as for example: "random.Next(5) == 0" is quicker then "random.Next(100) <= 20 Though - if the probability changes from the user input - then it would look like:

[ModifierByChoice] bool GetProbability(int trueProbability, int falseProbability)
{
var random = new Random();
return random.Next(trueProbability, trueProbability + falseProbability) < trueProbability;
}
1
  • By reducing the fractions, the numbers written in the code suddenly become magical numbers. Unless you can and actually do prove that there is a significant slowdown by using larger numbers, the advice of reducing the fractions is counterproductive. Furthermore it may confuse beginning programmers that you sometimes use the < operator and sometimes the ==, once even comparing to zero and the other time comparing to nonzero. Feb 11, 2019 at 18:22
-3
Random gen = new Random();
var boolVal = gen.Next(0, 1)==1? true : false;
1
  • The question was: But how can I generate a true false value with my own probability? Let's say it returns true with a probability of 40:60 or 20:80 etc.... This answer is wrong.
    – Pang
    May 3, 2016 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.