• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / JavaScript Random Boolean – How to Generate Random Boolean Values

JavaScript Random Boolean – How to Generate Random Boolean Values

May 10, 2022 Leave a Comment

In JavaScript, we can easily get a random boolean using the JavaScript Math.random() method along with an if else conditional statement.

var booleanValue;
if ( Math.random() > .5 ){
  booleanValue = true;
} else {
  booleanValue = false;  
}

Being able to generate random numbers efficiently when working with a programming language is very important. In JavaScript, we can generate a random number easily to get random boolean values.

To get a boolean randomly, we can use the JavaScript Math.random() method.

The random() method will generate a random float between 0 and 1. We can then put this in an if else condition statement. If the value is greater than .5 (50% chance of this), then we return true. If not, we return false.

Below is an example of how to get a boolean value randomly in JavaScript.

var booleanValue;
if ( Math.random() > .5 ){
  booleanValue = true;
} else {
  booleanValue = false;  
}

console.log(booleanValue);

#Output:
True

One such application of generating random boolean values would be if you wanted to generate a coin flip in JavaScript.

Below is some sample code of how you could flip a coin in JavaScript.

var headsOrTails;
if ( Math.random() > .5 ){
  headsOrTails = "Heads";
} else {
  headsOrTails = "Tails";
}

Using JavaScript to Generate a List of Random Booleans in a Loop

If you want to generate a list of random booleans, we can easily define a function and use a loop in JavaScript.

In this example, we will create a function that takes one argument, the number of booleans you want to create, and will return a list of random booleans.

Below is some sample code that will get the random booleans for you in JavaScript.

function randomBooleans(n){
  var bools = [];
  for ( var i = 0; i < n; i++ ){
    if ( Math.random() > .5 ){
      bools[i] = true;
    } else {
      bools[i] = false;  
    }
  }
  return bools;
};

console.log(randomBooleans(10));

#Output:
[True, True, False, True, True, False, True, True, False, True]

Generating a Random Boolean With Probability

In the examples above, we’ve been assuming that we want to have 50% True and 50% False generated from our JavaScript program.

If we want to create a random boolean based on probability, we can use the Math.random() method and adjust the threshold.

For example, if we want to generate True 70% of the time, then we would do the following in JavaScript:

var booleanValue;
if ( Math.random() > .7 ){
  booleanValue = true;
} else {
  booleanValue = false;  
}

Hopefully this article has been helpful for you to learn how to get a random boolean in JavaScript.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Check If String is a Number
  • 2.  Swapping Images in JavaScript
  • 3.  Clicking on an Image to Enlarge it in HTML
  • 4.  Changing the Background Image of a div in JavaScript
  • 5.  JavaScript join – Create String from Elements in an Array
  • 6.  Using JavaScript to Count Even Numbers in an Array
  • 7.  JavaScript indexOf – Get the position of a value within a string
  • 8.  How to Uncheck All Checkboxes in JavaScript
  • 9.  JavaScript toLocaleString – Display the Date and Time Using JavaScript
  • 10.  Using JavaScript to Convert Double to Integer

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy