Content · Programming · Python

Python random.getrandbits() Function: Usage and Examples

Create an integer with a requested number of random bits.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Create an integer with a requested number of random bits.

What It Does and Why It Matters


random.getrandbits(k) returns an integer in the interval from zero to 2**k - 1. The k argument must be nonnegative; zero bits produces zero. This method provides pseudorandom data and is not appropriate for cryptographic keys or secrets.

Use secrets.randbits() where unpredictable bits are required.

Pseudorandom examples are suitable for simulation and tests. Use secrets instead for tokens, passwords and authentication.

Step-by-Step Example


Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.

import random
number_generator = random.Random(5)
random_value = number_generator.getrandbits(12)
print(0 <= random_value < 2**12)
print(number_generator.getrandbits(0))

How the Code Works


  • The imports make these standard-library tools available: random.
  • The name number_generator receives the value of random.Random(5). Changing this input lets you explore a different case.
  • The print calls display the results. Compare these results with the method’s documented return type and the input used.
  • The dedicated Random instance isolates the generator state from unrelated random calls. A fixed seed is useful here for repeatable demonstrations but must not be reused to generate secrets.

Expected output:

True
0

Each displayed line comes from the corresponding calculation or check. The output helps verify the return values before you adapt the example to your own input.

What to Notice


Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.

If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.

Where You Would Use This


Use this in calculations, data summaries, simulations or validation when the numerical assumptions match the task. Check input domains, units and precision before interpreting a result. A single example output does not prove a statistical conclusion.

Practice Exercise


Generate four 8-bit integers and show their binary representations.

Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.

Reference: official Python documentation for the complete API contract and version notes.

Common Beginner Mistakes


  • Assuming getrandbits() produces a fixed-width displayed string or using its output for secure tokens.
  • Overlooking the documented condition: the k argument must be nonnegative; zero bits produces zero.

Key Takeaways


  • Create an integer with a requested number of random bits.
  • random.getrandbits(k) returns an integer in the interval from zero to 2**k - 1.
  • Generate four 8-bit integers and show their binary representations.
  • Pseudorandom examples are suitable for simulation and tests. Use secrets instead for tokens, passwords and authentication.
Share this resourceSend this page to someone who may find it useful.