[Tutor] What are these things urandom() returns?

Kent Johnson kent37 at tds.net
Tue Oct 10 12:04:25 CEST 2006

Dick Moores wrote:
> "urandom(n)
> Return a string of n random bytes suitable for cryptographic use.
> This function returns random bytes from an OS-specific randomness 
> source. The returned data should be unpredictable enough for 
> cryptographic applications, though its exact quality depends on the 
> OS implementation. On a UNIX-like system this will query 
> /dev/urandom, and on Windows it will use CryptGenRandom. If a 
> randomness source is not found, NotImplementedError will be raised. 
> New in version 2.4."
>  >>> from os import urandom
>  >>> urandom(10)
> '\xc0\xf0\xea\x0c\xdd\x95u at Z\x89'

It's a string where each character (byte) is selected randomly from the 
range 0-255. In Python a string can be used as a container for arbitrary 
byte values that are not text and maybe don't fit your notion of what a 
string is.

When the Python interpreter prints a value automatically (not as the 
result of an explicit print) as above, it prints repr(value). For a 
string s, repr(s) uses \x escapes to display any non-printable characters.

So what you see above is the repr() of a string whose first bytes 
contain the values (in hexadecimal)
75 == ord('u') so it prints as 'u' rather than \x75
40 == ord('@')
5a == ord('Z')


More information about the Tutor mailing list