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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 29 June

    Get Day Name from Date in Python

    Table of ContentsUse the Calendar Module to Get the Name of the Day in PythonUse the strftime() Method to Get the Name of the Day in PythonUse Pandas Timestamp Method to Get the Name of the Day in PythonUse the weekday() Method to Get the Name of the Day in PythonUse the isoweekday() Method to […]

  • 27 June

    Fill Array with Random Numbers in Python

    Table of ContentsFill array with random numbers in PythonWays to fill array with random numbers in PythonUsing the numpy.random.randint() function to fill array with random numbers in PythonUsing the numpy.random.Generator.integers() function to fill array with random numbers in PythonUsing the random.randint() function to fill array with random numbers in PythonConclusionWas this post helpful? Fill array […]

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.