Generating a large random string

Chris feb04.20.netman at spamgourmet.com
Thu Feb 19 14:50:32 EST 2004


How are you planning on using this large random string? If you are just
planning on printing, you can write:

for i in xrange(100000):
    sys.stdout.write(random.choice(string.letters))

and it will dump lots of garbage to the screen pretty quickly.  If you're
looking to do something else with the data, be specific about what you're
trying to do.

Chris

"Andreas Lobinger" <andreas.lobinger at netsurf.de> wrote in message
news:4034D506.2E37519A at netsurf.de...
> Aloha,
> i wanted to ask another problem, but as i started to build an example...
>
> How to generate (memory and time)-efficient a string containing
> random characters? I have never worked with generators, so my solution
> at the moment is:
>
> import string
> import random
> random.seed(14)
> d = [random.choice(string.letters) for x in xrange(3000)]
> s = "".join(d)
> print s
>
> which is feasible for the 3000, but i need lengths in the range
> 10.000 to 1.000.000.
>
> Hoping for an answer and wishing a happy day
> LOBI





More information about the Python-list mailing list