HOWTO · JavaScript

How to Generate Random Boolean in JavaScript

This article demonstrates how to generate random Boolean values in JavaScript using various methods. Learn to use Math.random(), bitwise operations, and integer conversions to create dynamic and engaging applications. Discover practical examples and explanations that enhance your programming skills.

On this page

To create a random true or false value in JavaScript, compare Math.random() with 0.5. The built-in method returns a number from 0 (inclusive) up to, but not including, 1, as documented in MDN’s Math.random() reference. The comparison divides that range into two equally sized parts.

Generate a random Boolean with Math.random()

This self-contained example returns true for values at least 0.5 and false for values below it:

function getRandomBoolean() {
    return Math.random() >= 0.5;
}

console.log(getRandomBoolean());
true

The call to Math.random() is made each time the function runs, so later calls may return either Boolean value.

Convert a random integer to a Boolean

Math.floor(Math.random() * 2) produces either the integer 0 or 1: multiplying scales the random range, and Math.floor() rounds down. Boolean() maps 0 to false and 1 to true:

Boolean(Math.floor(Math.random() * 2))

This is another native way to produce the two Boolean values. The threshold comparison above is shorter; both rely on Math.random().

Use bitwise operations to generate a Boolean

The expression below applies bitwise AND (&) to a scaled random number and 1. The double negation (!!) converts the numeric result to a Boolean:

!!(Math.random() * 1000 & 1)

This is an alternative, less direct expression. It uses AND, not OR, and does not provide a special performance advantage established here.

Choose a Boolean from an array

To select one of the two values from an array, use a random index. Multiplying by the array length and applying Math.floor() gives an index within the array for the values returned by Math.random():

const values = [true, false]; const result = values[Math.floor(Math.random() * values.length)];

This pattern also works for other arrays: replace values with the collection you want to sample.

Use Lodash’s _.sample(collection)

If Lodash is already available in your page or project, _.sample(collection) selects one item from a collection. For Boolean values, call it with [true, false]:

_.sample([true, false])

Make sure Lodash is loaded or imported before calling this method. A current CDN address is not verified here, so use the installation method documented for your project rather than relying on an old URL.

FAQ

How does Math.random() work in JavaScript?

Math.random() returns a floating-point number in the range [0, 1): 0 can occur, while 1 is excluded. Comparing the result with 0.5 maps values below the threshold to false and values at or above it to true.

Can I generate random Booleans without using Math.random()?

The approaches in this article all use Math.random() directly or through a helper library. For an application that needs security-sensitive random values, use the Web Crypto API’s Crypto.getRandomValues() rather than Math.random(); this article’s examples are for ordinary choices and simulations.

Is there a performance difference between these methods?

No performance measurements are supplied for these approaches. Choose based on readability and whether your project already uses Lodash.

Can I generate a Boolean with a specific probability?

Yes. With Math.random() < threshold, a threshold in [0, 1] gives the corresponding probability of true: for example, 0.2 gives a 20% chance. Values at or below 0 always produce false, while values at or above 1 always produce true, because Math.random() returns values in [0, 1) and the comparison is strictly <. A threshold of 0.5 gives equal chances to true and false.

Are there libraries for generating random values in JavaScript?

Lodash provides _.sample(collection), which can choose between true and false when given [true, false]. Lodash must be available in your page or project before you call it. The native Math.random() approaches need no additional library.