In today's post, I'm going to show you how to get a random truthy/falsy value in JavaScript. This can be helpful when you want to run some conditional statements in a randomized way. It can be done with a really simple helper function:
/**
* Get a random truthy/falsy value
* {@link https://gist.github.com/kieranbarker/25b1b4b4cef14c65b10dcefdc462f6ee}
* @returns {Number} 1 (truthy) or 0 (falsy)
*/
function fiftyFifty () {
return Math.round(Math.random());
}
You would use it like so:
if (fiftyFifty()) {
// This will run 50% of the time...
}
Let's see how it works.
Math.random()
The first thing we do is make a call to the static Math.random()
method. This returns a pseudo-random number between 0
(inclusive) and 1
(exclusive). This means that it could return 0
, or anything between 0
and 1
, but not 1
.
Here are some examples. I just ran Math.random()
in the console five times:
0.8424333633138914
0.6708640956019702
0.29226591753427367
0.9309103559864624
0.9709222745438715
Math.round()
Once we've got our random number, we pass it to the static Math.round()
method. This does what you'd expect: it returns the number after rounding it to the nearest integer. If the fractional part of the number is less than 0.5
, the number will be rounded down. If it's greater than or equal to 0.5
, the number will be rounded up.
Because Math.random()
always returns a number between 0
and 1
, our helper function will always return 0
(rounded down) or 1
(rounded up).
Truthiness and falsiness
In most (if not all) programming languages, a value is either truthy or falsy. Because 1
is truthy and 0
is falsy, this means you can call our helper function and use the returned value as a condition, as I demonstrated before:
if (fiftyFifty()) {
// This will run 50% of the time...
}
Returning an explicit boolean value
It's not necessary, but if you really wanted to return an explicit boolean value for some reason, you could force the conversion using a double NOT (!
) operator:
/**
* Get a random boolean value
* {@link https://gist.github.com/kieranbarker/25b1b4b4cef14c65b10dcefdc462f6ee}
* @returns {Boolean} True or false
*/
function fiftyFifty () {
return !!Math.round(Math.random());
}
The MDN Web Docs explain:
The logical NOT (
!
) operator ... takes truth to falsity and vice versa. It is typically used withBoolean
(logical) values. When used with non-Boolean values, it returnsfalse
if its single operand can be converted totrue
; otherwise, returnstrue
.
If Math.round(Math.random())
returns 0
, then the first NOT (!
) operator will convert it to true
, since !0
is true. The second NOT (!
) operator then turns it back to false
, since !true
is false.
If Math.round(Math.random())
returns 1
, then the first NOT (!
) operator will convert it to false
, since !1
is false. The second NOT (!
) operator then turns it back to true
, since !false
is true.
I first saw Steve Griffith use the Math.round(Math.random())
technique in one of his videos called Efficient ES6 async await with fetch, so credit to him for that.