Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a "big" random number in Python?

How can I generate a big (more than 64 bits) random integer in Python?

like image 344
Charles Brunet Avatar asked Apr 04 '12 13:04

Charles Brunet


People also ask

How do you generate a random large number in Python?

Random integers can be generated using functions such as randrange() and randint(). Let us first take a look at randint(). In case you want to generate numbers in intervals, you can use the randrange() function.

How do you randomly pick a number between 1 and 100 in Python?

print(randint(1, 100)) # Pick a random number between 1 and 100. x = randint(1, 100) # Pick a random number between 1 and 100.

How do you generate a random number in a dataset in Python?

Generating Random Integersrandom. randint() function returns a random integer between a and b (in this case, 1 and 500) which includes a and b, in other words: a<= x <=b. Whereas random. randrange() chooses a random item from that range (start=0, stop=500, step=5), which can be 0, 5, 10, 15 and so on, until 500.


1 Answers

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.

like image 102
Sven Marnach Avatar answered Sep 22 '22 03:09

Sven Marnach