Generating a large random string

Sean Ross sross at connectmail.carleton.ca
Thu Feb 19 18:54:32 EST 2004


[snip]
>
> # different approach
> seed(14)
> # add 1 so population > sample size (n)
> factor = n/len(letters) + 1
> start = now()
> s = ''.join(sample(letters*factor, n))
> took = now() - start
> print "new way n: %d took: %2.2fs"%(n, took)
[snip]

There's a problem with this method as it appears
random.sample is done without replacement, e.g.

>>> from random import sample
>>> sample(range(10), 10)
[9, 5, 3, 2, 0, 8, 4, 1, 6, 7]
>>>

Sorry about that,
Sean





More information about the Python-list mailing list