Generating a large random string

Roger Binns rogerb at
Fri Feb 20 03:51:09 CET 2004

> > How to generate (memory and time)-efficient a string containing
> > random characters?

It depends how random you need it to be.

The approach I take in my test harness (which generates a CSV file
with random contents) is to create a 30,000 character string the
old fashioned way:

  "".join([random.choice(item) for i in range(30000)])

item is a string of which characters to choose from (some fields
are phone numbers, some are names, some are email addresses etc).

To generate a random string I then take random slices of that
30,000 character object.  If I need longer strings, I glue
the random slices together (in a cStringIO).


More information about the Python-list mailing list