The random.getrandbits()
method is used to return a random number in the specified size, in bits.
random.getrandbits(n)
The random.getrandbits()
method requires a parameter value, n
, that specifies the size (in bits) of the random number to be generated.
random.getrandbits()
methodLet’s generate a random integer with a bit size of 4
.
import randomprint(random.getrandbits(4))
1
: We import the random
module.3
: We use the random.getrandbits()
method to generate a random number with a bit size of 4
.