Get random boolean in Python

Get random boolean in Python

What are Boolean Values?

Python has a built-in data type Boolean that represents True and False values. It has only these two values. We can also consider True and False as 1 and 0, respectively. If we convert 0 or 1 to Boolean, then we get True or False only.

This tutorial will demonstrate how to generate random Boolean values in Python.

Ways to Generate Random Boolean in Python

Random numbers are very instrumental in programming. We tend to use them for generating test cases or simulations. There are several methods associated with random number generation in Python. We will discuss how to generate random Boolean values.

Using the random.getrandbits() function

The getrandbits method of the random module returns the integers or numbers with a specified number of bits. The number of bits returned is placed as an argument or a parameter of this function.

For example,

Output:

True

In the above example,

  • We pass one as the parameter of the getrandbits() function. This will generate either 0 or 1.
  • Then, we use the bool() function to convert this randomly generated integer to a Boolean value, i.e., True or False and displayed it.

Using the random.choice() function

With the random.choice() function, we can randomly select an element from a given sequence like a list.

To generate random Boolean values using this function, we will create a list of two elements, True and False and then randomly select a value from this list.

For example,

Output:

True

We use the bool() function in the above example also. This function ensures that the final value is of type bool.

Using the random.random() function

The random.random() function generates a random float number between 0 and 1. We can use it to generate random Boolean values.

For example,

Output:

False

In the above code,

  • We generate a random float value using the random.random() function.
  • Then, we compare this value with 0.5. If the value is greater than 0.5, it will return True, or it will return False.
  • This way, by comparing it with 0.5, we get a random Boolean value.

Using the random.randint() function

This method is similar to the previously discussed random.getrandbits() method.

We will use the random.randint() function to get a random integer, 0 or 1. Then we convert this to Boolean using the bool() function.

For example,

Output:

True

Using the numpy.random.choice() function

The numpy module has a variety of random functions to generate random arrays of specific sizes. The numpy.random.choice() function returns a random set of samples in a one-dimensional array that returns a random numpy array. This method will create a random Boolean array in this method.

For example,

Output:

[ True True False True False]

In the above example,

  • We generate an array of random Boolean values having a total of five elements.
  • The map() function applies the bool() method to all the elements of the array.
  • The list() function converts the map object to a list.

Using the numpy.random.rand() function

The numpy.random.rand() function generates an array of the given size with random float values. We will follow a similar logic as discussed in the random.random() method.

For example,

Output:

[False False True True True]

Using the numpy.random.randint() function

This function will create an array of random integers between a given range.

We will generate an array of random integers, either 0 or 1, and convert them to Boolean values using the bool() function.

For example,

Output:

[ True True False True False]

Using the faker.pybool() function

The faker library can generate various types of fake test data. We first create an object using the Faker() constructor and the pybool function to get a random Boolean value.

For example,

Output:

True

Conclusion

In this article, we discussed how to generate random Boolean values in Python. Different methods involving the random, numpy, and faker() module were discussed. The functions of the numpy module are more hopeful in generating arrays of such values.

Related Posts

  • Truncate float in Python
    02 November

    Truncate float in Python

    Table of ContentsHow to truncate float in Python?Using f-strings to truncate float in Python.Using the int() function to truncate float in Python.Using the str() function to truncate float in Python.Using the round() function to truncate float in Python. Truncating a floating-point number is a widely used practice and helps in making the calculations and results […]

  • Python for loop increment by 2
    01 November

    For Loop Increment By 2 in Python

    Table of ContentsPython For Loop Increment By 2 Using the range() FunctionPython For Loop Increment By 2 Using List SlicingConclusion We often use a for loop in python to iterate over a container object like a list or tuple. We also use for loops to perform tasks a fixed number of times. In python, the […]

  • Check if input is integer in Python
    01 November

    Check if input is integer in Python

    Table of ContentsUsing the isdigit() method to check if input is integer in Python.Using the isnumeric() method to check if input is integer in Python.Using exception handling along with the int() function to check if input is integer in Python.Using Regular Expression to check if input is integer in Python. There is wide use of […]

  • How to decrement for loop in Python
    28 October

    How to decrement for loop in python

    Table of ContentsWhat is for loop in Python?Ways to decrement the for loop in PythonUsing the start, stop and step parameters in range() functionUsing the reversed() functionUsing the while loopConclusion We use the for loop widely in the programming world. Most programs, whether highly complex or not, contain this loop. Sometimes, depending on the conditions, […]

  • 25 October

    Increment operator in python

    Table of ContentsPython’s distinct way of using the increment operatorWays to increment a variable in PythonUsing the augmented assignment operatorUsing the step parameter in the range() functionThe role of immutability in Python The increment process is among the basic and the essentials of any programming language. We use it in problem-solving and when there is […]

  • How to comment out multiple lines in python
    16 October

    How to comment out multiple lines in python

    Table of ContentsMultiple Line Comments in PythonUsing the # characterUsing triple quotesUsing IDEs and Text Editors In this article, we will see about how to comment out multiple lines in python. The concept of comments is present in most programming languages. We use comments for documentation purposes. The compiler ignores them, but the user can […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.