Python 3 os.urandom

24,552

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/

Share:
24,552
John
Author by

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, 2020

Comments

  • John
    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
    Danica over 11 years
    random.choice would also probably be a reasonable option.
  • Tim
    Tim over 11 years
    random.choice works too, depending on what he ultimately wants to do with the random character.
  • John
    John over 11 years
    random is a non-cryptographic PRNG, i need a cryptographic quality random
  • Tim
    Tim over 11 years
    You can use random.SystemRandom instead of random. It makes use of os.urandom() and provides all the same functionality as random.
  • Charlie Parker
    Charlie Parker almost 8 years
    Is there no way to get a cryptographically secure integer form the OS though?
  • Charlie Parker
    Charlie Parker almost 8 years
    I 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
    Charlie Parker almost 8 years
    Why 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
    Charlie Parker almost 8 years
    Do 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
    Charlie Parker almost 8 years
    To answer my own question, yes. os.urandom does that. docs.python.org/2/library/os.html
  • Charlie Parker
    Charlie Parker almost 8 years
    @Tim you can get the seed nor anything from random.SystemRandom([seed]), what is the point of it?
  • eafit
    eafit over 7 years
    in python3, you need to use rand=int.from_bytes(os.urandom(4), sys.byteorder)