Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

random.randint(1,n) in Python

Tags:

python

random

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

like image 404
Arkapravo Avatar asked Apr 08 '10 03:04

Arkapravo


1 Answers

No doubt you have a bounded amount of memory, and address space, on your machine; for example, for a good 64-bit machine, 64 GB of RAM [[about 2**36 bytes]] and a couple of TB of disk (usable as swap space for virtual memory) [[about 2**41 bytes]]. So, the "upper bound" of a Python long integer will be the largest one representable in the available memory -- a bit less than 256**(2**40) if you are in absolutely no hurry and can swap like crazy, a bit more than 256**(2*36) (with just a little swapping but not too much) in practical terms.

Unfortunately it would take quite a bit of time and space to represent these ridiculously humongous numbers in decimal, so, instead of showing them, let me check back with you -- why would you even care about such a ridiculous succession of digits as to constitute the "upper bound" you're inquiring about? I think it's more practical to put it this way: especially on a 64-bit machine with decent amounts of RAM and disk, upper bounds of long integers are way bigger than anything you'll ever compute. Technically, a mathematician would insist, they're not infinity, of course... but practically, they might as well be!-)

like image 131
Alex Martelli Avatar answered Oct 07 '22 08:10

Alex Martelli