How to generate a "big" random number in Python?
You can also use this function to generate a number of any length.
def generateRandomNumber(digits):
finalNumber = ""
for i in range(digits // 16):
finalNumber = finalNumber + str(math.floor(random.random() * 10000000000000000))
finalNumber = finalNumber + str(math.floor(random.random() * (10 ** (digits % 16))))
return int(finalNumber)
You can use random.getrandbits()
:
>>> random.getrandbits(128)
117169677822943856980673695456521126221L
As stated in the linked documentation, random.randrange()
will also do the trick if random.getrandbits()
is available.