C# Random Bool

Harshit Jindal Oct 12, 2023
  1. Use the Next() Method From C# Class Random
  2. Use the NextDouble() Method From C# Class Random
C# Random Bool

Have you come across a situation where you have to flip a coin programmatically? Have you ever wanted to distribute things between two databases randomly?

The thing that you are trying to achieve is what we call a random boolean. A boolean variable has only two possible values, true or false.

When we randomly generate this boolean, we get a random boolean. This tutorial will show how to create a random boolean using C#.

We will also see the fastest and most ideal way to generate it.

Use the Next() Method From C# Class Random

The Random class in C# provides us with an arsenal of randomness. It gives a pseudo-random generator that can produce a random sequence based on statistical requirements.

Although the class explicitly doesn’t provide a method for boolean as it does for bytes, integers, and doubles. But it can efficiently use it to generate them since false and true are nothing but 0 and 1 from programmers’ perspectives.

The first way is to use the Next() method to generate random integers in the range [0,n) by allocating n as 2 and reducing the capacity to constitute only 0 and 1.

// Generate n random booleans using C# class Random
using System;

public class RandomBoolean {
  public static void Main() {
    int n = 5;
    var random = new Random();
    for (int i = 0; i < n; i++) {
      Console.WriteLine(random.Next(2) == 1);
    }
  }
}

Output:

False
True
True
False
False

Use the NextDouble() Method From C# Class Random

We saw how we could use Next() to generate a random boolean in the previous example. In this approach, we will see how we can use another method, NextDouble() from the same class, Random.

The NextDouble() returns a random double value between 0.0 and 1.0. So, we can add a separator at any place between these two numbers and divide the numbers generated into true or false based on a separation condition.

For example, let us select the double value 0.3 as separator and the separation condition as generated number >= 0.3. So if the number satisfies the condition, we get true, else false.

To achieve sufficiently random distribution, the preferred value of the separator is 0.5.

Output:

True
False
True
True
False
// Note this is random and may not match with your actual output

Both the methods described in the post are reasonably fast.

But suppose we have to pick the fastest. In that case, the second method seems shorter.

The method Next() internally returns (int)(this.Sample()*maxValue) compared to just this.Sample() by NextDouble() leading to the additional overhead of multiplication and casting.

Harshit Jindal avatar Harshit Jindal avatar

Harshit Jindal has done his Bachelors in Computer Science Engineering(2021) from DTU. He has always been a problem solver and now turned that into his profession. Currently working at M365 Cloud Security team(Torus) on Cloud Security Services and Datacenter Buildout Automation.

LinkedIn

Related Article - Csharp Random

Related Article - Csharp Boolean