Generate a pseudo-random boolean

You can generate a pseudo-random boolean in JavaScript or PHP using a single line of code. No, you do not need to install yet another idiotic JavaScript package.

In JavaScript:

const random = Math.random() <= 0.4999

And in PHP:

$random = (mt_rand(0, 1) === 0)

If you need a cryptographically-secure random boolean in PHP, use random_int instead:

$secure = (random_int(0, 1) === 0)

Sign up for my newsletter

A monthly round-up of blog posts, projects, and internet oddments.