Random strings in Python 2.6 (Is this OK?)

PythonRandomPython 2.6

Python Problem Overview


I've been trying to find a more pythonic way of generating random string in python that can scale as well. Typically, I see something similar to

''.join(random.choice(string.letters) for i in xrange(len))

It sucks if you want to generate long string.

I've been thinking about random.getrandombits for a while, and figuring out how to convert that to an array of bits, then hex encode that. Using python 2.6 I came across the bitarray object, which isn't documented. Somehow I got it to work, and it seems really fast.

It generates a 50mil random string on my notebook in just about 3 seconds.

def rand1(leng):
    nbits = leng * 6 + 1
    bits = random.getrandbits(nbits)
    uc = u"%0x" % bits
    newlen = int(len(uc) / 2) * 2 # we have to make the string an even length
    ba = bytearray.fromhex(uc[:newlen])
    return base64.urlsafe_b64encode(str(ba))[:leng]

edit

heikogerlach pointed out that it was an odd number of characters causing the issue. New code added to make sure it always sent fromhex an even number of hex digits.

Still curious if there's a better way of doing this that's just as fast.

Python Solutions


Solution 1 - Python

import os
random_string = os.urandom(string_length)

and if you need url safe string :

import os
random_string = os.urandom(string_length).hex() 

(note random_string length is greatest than string_length in that case)

Solution 2 - Python

Sometimes a uuid is short enough and if you don't like the dashes you can always.replace('-', '') them

from uuid import uuid4

random_string = str(uuid4())

If you want it a specific length without dashes

random_string_length = 16
str(uuid4()).replace('-', '')[:random_string_length]

Solution 3 - Python

Taken from the 1023290 bug report at Python.org:

junk_len = 1024
junk =  (("%%0%dX" % junk_len) % random.getrandbits(junk_len *
8)).decode("hex")

Also, see the issues 923643 and 1023290

Solution 4 - Python

It seems the fromhex() method expects an even number of hex digits. Your string is 75 characters long. Be aware that something[:-1] excludes the last element! Just use something[:].

Solution 5 - Python

Regarding the last example, the following fix to make sure the line is even length, whatever the junk_len value:

junk_len = 1024
junk =  (("%%0%dX" % (junk_len * 2)) % random.getrandbits(junk_len * 8)).decode("hex")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmikelikespieView Question on Stackoverflow
Solution 1 - PythonSeun OsewaView Answer on Stackoverflow
Solution 2 - PythonJoelbitarView Answer on Stackoverflow
Solution 3 - PythonfdrView Answer on Stackoverflow
Solution 4 - PythonunbeknownView Answer on Stackoverflow
Solution 5 - Pythonuser115995View Answer on Stackoverflow