Generating a large random string

Sean Ross sross at
Fri Feb 20 02:29:33 CET 2004

"Paul Rubin" <> wrote in message
news:7xsmh6a7cd.fsf at
> import array
> from random import randint
> d = array.array('B')
> for i in xrange(1000000):
>   d.append(randint(0,255))
> s = d.tostring()


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