os.urandom()
returns bytes, so you should just reinterpret them as a long
or int
; you chose to convert it first to hexadecimal and then further convert it to int
; this works fine and will work for arbitrary long numbers. You could have used struct
to achieve this in one step, but this will not work for very large numbers, so I think that step is just fine (unless you want to do this millions of times each second).
You should strip that random.shuffle
part, though. It does not add any "randomness" to your values. Just use str(ri)[-n:]
if you need a string of length n
.