Generating a large random string

Paul Rubin http
Thu Feb 19 22:57:54 EST 2004


"Sean Ross" <sross at connectmail.carleton.ca> writes:
> And when you've done
> 
> s = open("/dev/urandom").read(1000000)
> 
> is s a string containing one million letters [a-zA-Z] and no other
> types of characters, as the OP is looking for?

Oops, per other post, it gives strings of bytes and needs filtering.
The following runs in about 1.2 seconds on my machine, but has an
small (infinitesimal) chance of failure:

import string,array,time
t=time.time()
ttab = string.letters*4 + '\0'*48
a = array.array('B', open("/dev/urandom").read(1500000).translate(ttab))
a = array.array('B', filter(abs,a)).tostring()[:1000000]
print time.time()-t



More information about the Python-list mailing list