Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random 32 hexadecimal digits in Python

Tags:

python

hex

I am trying to find a great way to produce a 32 digit hex sequence that is random and gets its randomness from a Big Number like 10*78.

For Python code I also found this:

ran = random.randrange(10**80)
myhex = "%030x" % ran

This produces a 64 digit hex string, BUT sometimes the result is 63 or even 61 characters and I'm not sure why? I need it to return 64 exactly each time.

I need helping tweaking the above code so that it ALWAYS returns 64 only. I do not know enough about the how the "%030x" part works.

I've also tried producing a longer hex string and shortening it with:

myhex = myhex[:64]

But I was still seeing strings that were less than 64 being returned.

In the end I just need exactly 64 hexadecimal string derived from a Big Number like 10*78, returned each time.

Solution I went with

import random

ran = random.randrange(10**80)
myhex = "%064x" % ran

#limit string to 64 characters
myhex = myhex[:64]

print(myhex)

Seems @ScottMillers answer below was the key I needed to find the answer so I'm selecting his answer as the solution, but wanted to post my final code that worked for me here.

Thanks everyone

like image 443
Norman Bird Avatar asked Apr 12 '16 17:04

Norman Bird


People also ask

How do you generate a random hexadecimal number in Python?

In this code at first, we import the random library to work. Then we generate a random integer decimal number by using random. randint() function, which must be in between lower value 0 and upper value 16777215. Then we convert the decimal number into hexadecimal value.

How do you generate random numbers in Python?

Random integer values can be generated with the randint() function. This function takes two arguments: the start and the end of the range for the generated integer values. Random integers are generated within and including the start and end of range values, specifically in the interval [start, end].

How do you use hexadecimal in Python?

When denoting hexadecimal numbers in Python, prefix the numbers with '0x'. Also, use the hex() function to convert values to hexadecimal format for display purposes. Our two hexadecimal code samples are similar to the ones we used for binary.

Does Python support hexadecimal?

Python hex() function is used to convert an integer to a lowercase hexadecimal string prefixed with “0x”. We can also pass an object to hex() function, in that case the object must have __index__() function defined that returns integer. The input integer argument can be in any base such as binary, octal etc.


Video Answer


2 Answers

In modern Python distributions, the new library secrets makes it easy and might be what you need. From the docs:

The secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets.

In particularly, secrets should be used in preference to the default pseudo-random number generator in the random module, which is designed for modeling and simulation, not security or cryptography.

For Python 3.6 and up, try this:

import secrets
secrets.token_hex(32)
#'17450056b398cbeda1fa7e4fc25d4fb169ade2b1266892e2cecab56b12fb900b'

https://docs.python.org/3/library/secrets.html#module-secrets

For earlier versions of Python, there is a backport available (python2-secrets).

   pip install python2-secrets
like image 95
sww314 Avatar answered Nov 15 '22 00:11

sww314


The UUID module can return a 32 hexadecimal digits object:

import uuid
uuid.uuid4().hex
# '7dccf8a43a6c4e018caadacdb2d0f6f0'
like image 36
dyltini Avatar answered Nov 15 '22 00:11

dyltini