Generating a large random string

Jeff Epler jepler at unpythonic.net
Thu Feb 19 19:11:47 EST 2004


I'm pretty sure that the two methods you mention don't give results with
the same distribution.

Instead of letters, let's choose from "01", and choose a string of
length 2.

def f1():
    return ''.join([choice("01") for i in xrange(2)])

def f2():
    return ''.join(sample("01" * 2, 2))

def test():
    for f in (p1, p2):
        p = {}
        for i in range(1000):
            o = f()
            p[o] = p.get(o, 0) + 1
        print p

[jepler at parrot src]$ ./python /tmp/ross.py 
{'11': 25212, '10': 24904, '00': 24996, '01': 24888}
{'11': 16800, '10': 33289, '00': 16705, '01': 33206}

As you can see p1 picks evenly from the 4 alternatives, while p2 favors
the outcomes that have different contents (01 and 10) over the ones that
are a repetition of the same symbol (00 and 11).


Jeff




More information about the Python-list mailing list