[Tutor] help with random.randint (cont. -- now: pseudo code)

Wayne Werner waynejwerner at gmail.com
Wed Feb 3 12:37:55 CET 2010


On Wed, Feb 3, 2010 at 12:12 AM, David <ldl08 at gmx.net> wrote:

> def createQuestions:
>    generate all multiplication combinations possible
>    append as tuple to pool
>    eliminate 'mirrored doubles' (i.e. 7x12 and 12x7)
>    randomize pool
>
>
I haven't really looked through most of this stuff - but your mirrored
doubles has a somewhat cleaner solution than generating all then removing
the duplicates, I think.

Referring to here:
http://docs.python.org/reference/expressions.html#notin

I *think* you can create a list of all the non-mirrored pairs this way:

pool = []
for x in xrange(1,13):
    for y in xrange(x, 13):
        pool.append((x,y))

Then I would shuffle the pairs and the pairs within:

i.e.:

for x in xrange(len(pool)):     # We want the index, not just the element
    if random.randint(0,1):
         pool[x] = pool[x][::-1]    # A simple reversal swaps the values
    else:
        pass

now you have two options - either shuffle your pool, or pick random elements
and pop them out of your list:

(this worked at least once on a 10 element list):

while pool:
    pool.pop(random.randint(0, len(pool)-1)))

of course you'd probably assign that tuple to some useful value or function
call.

But that's what I'd do... and given the fact that I was actually planning to
make a "game" somewhat like this myself, this gives me a good excuse to
write some of the code ^_^

HTH, and thanks for asking a question that motivates,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100203/a5bd132d/attachment.htm>


More information about the Tutor mailing list