Python 3 os.urandom
Solution 1
If you just need a random integer, you can use random.randint(a, b)
from the random module.
If you need it for crypto purposes, use random.SystemRandom().randint(a, b)
, which makes use of os.urandom()
.
Example
import random
r = random.SystemRandom()
s = "some string"
print(r.choice(s)) # print random character from the string
print(s[r.randrange(len(s))]) # same
Solution 2
Might not exactly be on topic, but I want to help those coming here from a search engine. To convert os.urandom
to an integer I'm using this:
import os
rand = int(int(str(os.urandom(4), encoding="UTF-8")).encode('hex'), 16)
# You can then 'cycle' it against the length.
rand_char = chars_list[rand % 80] # or maybe '% len(chars_list)'
Note: The range of the index here is up to that of a 4-byte integer. If you want more, change the 4
to a greater value.
The idea was taken from here: https://pythonadventures.wordpress.com/2013/10/04/generate-a-192-bit-random-number/
John
New to Ubuntu, but eager to learn. Teaching myself to program as we speak, learning Java and python, if anyone has any suggestions or ideas for tutorials let me know, some of the tutorials have not been as accurate as i would like (the code from them didn't work).
Updated on November 19, 2020Comments
-
John over 3 years
Where can I find a complete tutorial or documentation on
os.urandom
? I need to get get a random int to choose a char from a string of 80 characters. -
Danica over 11 years
random.choice
would also probably be a reasonable option. -
Tim over 11 years
random.choice
works too, depending on what he ultimately wants to do with the random character. -
John over 11 yearsrandom is a non-cryptographic PRNG, i need a cryptographic quality random
-
Tim over 11 yearsYou can use
random.SystemRandom
instead ofrandom
. It makes use ofos.urandom()
and provides all the same functionality asrandom
. -
Charlie Parker almost 8 yearsIs there no way to get a cryptographically secure integer form the OS though?
-
Charlie Parker almost 8 yearsI am assuming that 16 is a argument to the built in function int that tells python that the given string is in base 16 (hexadecial).
-
Charlie Parker almost 8 yearsWhy did you give urandom 4 and not any other number? I know that 4 indicates a string of n random bytes suitable for cryptographic use, however, I keep seeing examples on the web where 4 is the the chosen number instead of say, 32 or 64. Why didn't you choose 64 say? More numbers more randomness/harder to predict, right?
-
Charlie Parker almost 8 yearsDo you might explaining some of the details of your code? I have written above the details I could find out by reading the docs but wanted to know if there are any caveats (and how to fix them) or some important comments to give the readers when trying to re-use your code to optimize randomness and/or security. Thanks.
-
Charlie Parker almost 8 yearsTo answer my own question, yes. os.urandom does that. docs.python.org/2/library/os.html
-
Charlie Parker almost 8 years@Tim you can get the seed nor anything from
random.SystemRandom([seed])
, what is the point of it? -
eafit over 7 yearsin python3, you need to use
rand=int.from_bytes(os.urandom(4), sys.byteorder)