Numbers over 32, 64 bits

Asked

Viewed 359 times

2

How to work with numbers above 32, 64 bits? The idea is to generate a large pseudo random numbers up to 4096, for example, by a pseudorandom number generator that generates numbers of 32.

In C I could allocate a corresponding space, working and putting the numbers there. In Python I thought about using array, but I need the final number to become one. Anyone have any idea?

3 answers

4

Complementing @Pedro’s response a little bit, there is currently no limit value for integers. You can see this by citation in the documentation:

The sys.maxint Constant was Removed, Since there is no longer a limit to the value of integers

Translating:

The sys.maxint constant has been removed as there is no limit to integer values

Let’s say you had something to match 2^4096 to store. So this would be perfectly calculable in python:

>>> 2**4096
1044388881413152506691752710716624382579964249047383780384233483283953907971557456848826811934997558340890106714439262837987573438185793607263236087851365277945956976543709998340361590134383718314428070011855946226376318839397712745672334684344586617496807908705803704071284048740118609114467977783598029006686938976881787785946905630190260940599579453432823469303026696443059025015972399867714215541693835559885291486318237914434496734087811872639496475100189041349008417061675093668333850551032972088269550769983616369411933015213796825837188091833656751221318492846368125550225998300412344784862595674492194617023806505913245610825731835380087608622102834270197698202313169017678006675195485079921636419370285375124784014907159135459982790513399611551794271106831134090584272884279791554849782954323534517065223269061394905987693002122963395687782878948440616007412945674919823050571642377154816321380631045902916136926708342856440730447899971901781465763473223850267253059899795996090799469201774624817718449867455659250178329070473119433165550807568221846571746373296884912819520317457002440926616910874148385078411929804522981857338977648103126085903001302413467189726673216491511131602920781738033436090243804708340403154190336L

See this little example in Ideone

1

>>> import random
>>> random.randint(0, 2**64)
6502449964907846195

Python automatically handles large numbers, and you don’t have to worry about the maximum bit value of a 32-bit or 64-bit integer.

  • Right. But I need to create through some algorithms for the purpose of studying them. For example, I will use 3 algorithms: Park-Miller, Xorshfit, Linear Congruential Generator. Using these 3 algorithms I have to be able to generate a very large number.

1


Although the Python Random module is very practical, for encryption purposes the recommendation is to use the call os.urandom(), that has randomness most guaranteed by the operating system.

On the other hand it is even more convenient, because the os.urandom already accepts a parameter with the number of random bytes and returns an object of type bytes - in this case, for 4096 bits, you use os.urandom(512) - in general for any use you are going to make the bytes object will be more practical than an integer with 4096 bits.

However, the language supports whole numbers of this size - so if you want the numeric value you can do: int.from_bytes(os.urandom(512), "big"). If you prefer as a text string with 2 hexadecimal digits per byte, you can do os.urandom(512).hex() (convertible back to bytes with bytes.fromhex(minha_string) ).

Now, let’s assume that you have another function of your choice that generates 32-bit random numbers of fixed size (for example, a call to a driver from a randomized number generator harware device) - in this case, you can use the "struct" module of Python to join 16 calls to its function in a continuous byte object with 512 bytes (and there you can transform into integer, or a hexadecimal string, as above):

struct.pack("=16I", *(random.randrange(0, 2**32) for _ in range(16)))

(In this case I used Random.randrange as an example, but the recommendation is to actually use.urandom, as I explained above). Understanding this expression: is a call to the function struct.pack where the first parameter describes the next ones: plus 16 32-bit integers that will be interpreted using the endiannes machine native - little endian in x86_64 - indicated by the "=" sign. Next a Generator Expression that is deployed by the operator * - each generated element is passed to the function as a positional parameter. The expression in turn simply repeats 16 times, given in for _ in range(16) the call the desired function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.