tags:

views:

138

answers:

3

Hello,

I am thinking of giving one or more set of introductory lectures to introduce people in my department with Python and related scientific tools as I did once in the last summer py4science @ UND.

To make the meetings more interesting and catch more attention I had given two Python learning materials to one of the lucky audience via the shown ways:

1-) Get names and assign with a number and pick the first one as the winner from the assigned dictionary.

import random 
lucky = {1:'Lucky1',...}
random.choice(lucky.keys())

2-) Similar to the previous one but pop items from the dictionary, thus the last one becomes the luckiest.

import random 
lucky = {1:'Lucky1',...}
lucky.pop(random.choice(lucky.keys()))

Right now, I am looking at least for one more idea that will have randomness inherently and demonstrate a useful language feature helping me to make a funnier lottery time at the end of one of the sessions.

+3  A: 

Cards are also a source of popular (and familiar!) games of chance. Perhaps you could show how easy it is to generate, shuffle and sample cards:

#!/usr/bin/env python
import random
import itertools

numname={1:'Ace',11:'Jack',12:'Queen',13:'King'}
suits=['Clubs','Diamonds','Hearts','Spades']
numbers=range(1,14)
cards=['%s-%s'%(numname.get(number,number),suit)
       for number,suit in itertools.product(numbers,suits)]
print(cards)
random.shuffle(cards)
print(cards)
hand=random.sample(cards,5)
print(hand)
unutbu
Smart indeed :) I could have people pick a card from a real card deck and compare what they have in hand and what the result come from using your script. Either the first match win or pop the names till exhaust the cards.
Gökhan Sever
A: 

Computing Pi is always fun ;-)

import random

def approx_pi( n ):
    # n random (x,y) pairs (as a generator)
    data = ( (random.random(),random.random()) for _ in range(n) )
    return 4.0*sum( 1 for x,y in data if x**2 + y**2 < 1 )/n

print approx_pi(100000)
THC4k
And how can I use this idea in my lottery?
Gökhan Sever
+1  A: 

One of the cutest uses of random numbers for mid-sized crowds is finding cycles. I will describe the physical method, and then some explorations. The Python code is fairly trivial.

Start with your group of about 100 people with their names on pieces of paper in a bowl. Everyone descends on the bowl and takes a random piece of paper. Each person goes to the person with that name. This leads to groups clumping together in various sizes. Not always what people expect.

For example, if Alice picks Bob, Bob picks Charlie, and Charlie picks Alice, then these three people will end up in their own clump. For some groups, have people join hands with their matches to see everyone being pulled this way and that. Also to see how the matches create chains or clumps.

Now write software to watch the number of clumps. Do the match on clumps, asking, for example, "how often is the biggest clump less than half the people"? For example, for N students, an average of 1/N will draw their own names.

Do you need code?

Charles Merriam
I am guessing a relatively longer code needs to written to implement your idea. Could you also clarify how will I pick a lucky person using this method? I am little lost in your explanation. Thank you.
Gökhan Sever
Each person puts his or her own name in the bowl. Each person draws one name from the bowl. Each person finds holds hands with the name on the drawn slip. I'll need to leave a second comment with some code.
Charles Merriam