Generating a large random string

Sean Ross sross at connectmail.carleton.ca
Thu Feb 19 20:29:33 EST 2004


"Paul Rubin" <http://phr.cx@NOSPAM.invalid> wrote in message
news:7xsmh6a7cd.fsf at ruckus.brouhaha.com...
[snip]
>
> import array
> from random import randint
> d = array.array('B')
> for i in xrange(1000000):
>   d.append(randint(0,255))
> s = d.tostring()

Hi.

Actually, that is pretty slow: 42.79s (on my machine)

The OP's solution took 23.94s
My sampling with replacement took 7.68s

No doubt someone else will come up with something faster.

I'd be interested to see how

s = open("/dev/urandom").read(3000)

compares, and, if better, whether something similar can
be done on Windows.









More information about the Python-list mailing list