Random.randint (1, n) in Python

Most of us know that the random.randint(1,n) command in Python (2.XX) will generate a number in a random (pseudo-random order) between 1 and n. I am interested to know what is the upper limit for n?

+7
python random
source share
2 answers

randint() works with long integers, so there is no upper limit:

 >>> random.randint(1,123456789012345678901234567890) 113144971884331658209492153398L 
+10
source share

Without a doubt, you have limited memory and address space on your computer; for example, for a good 64-bit machine, 64 GB of RAM [[about 2**36 bytes]] and several TB disks (used as a swap space for virtual memory) [[about 2**41 bytes]]. Thus, the "upper bound" of a long Python integer will be the largest represented in available memory - a little less than 256**(2**40) , if you are absolutely not in a hurry and can trade like crazy, a little more than 256**(2*36) (with a small replacement, but not too much) in practice.

Unfortunately, to represent these ridiculously huge numbers in decimal will take quite a lot of time and , therefore, instead of showing them, let me check with you - why would you even care about such a ridiculous sequence of numbers that makes up the "top" border "about which you ask? I think this is more practical, so to speak: especially on a 64-bit machine with a decent amount of RAM and disk, the upper bounds of long integers are way larger than anything you have ever calculated. Technically, a mathematician would insist, of course, they are not endless ... but in practice they can be like that! -)

+2
source share

All Articles