Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168737
Image: ubuntu2004

Here is a traditional game we can use to illustrate probability:

var('Nun, Gimel, Hei, Shin') dreidel = [Nun, Gimel, Hei, Shin]
choice(dreidel)
Hei

Every time you evaluate the above cell, Sage will randomly choose one of the sides of a virtual dreidel.

Let's spin it a bunch of times:

for spin in range(10): choice(dreidel)
Nun Shin Shin Nun Gimel Shin Gimel Gimel Nun Nun

Here is a traditional interpretation of the choices:

Nun : None

Gimel : Gimme

Hei : Hey!  I only get half?

Shin : Oh shin!

players = [3]*3 pot = 0
print players, pot; print for i in range(len(players)): if players[i] > 0: players[i] -= 1 pot += 1 print players, pot; print for i in range(len(players)): if pot == 0: for j in range(len(players)): if players[j] > 0: players[j] -= 1 pot += 1 print; print players, pot; print if players[i] > 0: spin = choice(dreidel) if spin == Gimel: players[i] += pot pot = 0 elif spin == Hei: players[i] += (pot+1)//2 pot //= 2 elif spin == Shin: players[i] -= 1 pot += 1 print 'player', i+1, ':', spin, '--->', players, pot
[5, 1, 0] 3 [4, 0, 0] 5 player 1 : Gimel ---> [9, 0, 0] 0 [8, 0, 0] 1

Here is another fun game based on probability:

coin = ['Heads - I win!', 'Tails - you lose!']
choice(coin)
'Heads - I win!'

This is an excellent game.  I really like it, because every time I play it, I win!  : )

Here is an easy way to simulate a die in Sage:

die = [1..6]
choice(die)
1

Wow, that sounds really existential!

Here we have a virtual 26-sided die:

alpha = 'abcdefghijklmnopqrstuvwxyz'
choice(alpha)
'a'

Let's throw a pair of six-sided dice and plot the results:

number_of_throws = 10000 throws = [(choice(die), choice(die)) for throw in range(number_of_throws)]
sums = [sum(throw) for throw in throws] frequencies = [0, 0]+[sums.count(outcome) for outcome in set(sums)] frequencies
[0, 0, 270, 592, 794, 1095, 1459, 1622, 1340, 1084, 885, 566, 293]
bar_chart(frequencies, xmin = 2, xmax = 13, ymin = 0, width = 1)

Here is a way to represent a deck of cards:

var('A C D H J K Q S') suits = [S, D, C, H] ranks = [A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K] deck = [(rank, suit) for suit in suits for rank in ranks]
deck
[(A, S), (2, S), (3, S), (4, S), (5, S), (6, S), (7, S), (8, S), (9, S), (10, S), (J, S), (Q, S), (K, S), (A, D), (2, D), (3, D), (4, D), (5, D), (6, D), (7, D), (8, D), (9, D), (10, D), (J, D), (Q, D), (K, D), (A, C), (2, C), (3, C), (4, C), (5, C), (6, C), (7, C), (8, C), (9, C), (10, C), (J, C), (Q, C), (K, C), (A, H), (2, H), (3, H), (4, H), (5, H), (6, H), (7, H), (8, H), (9, H), (10, H), (J, H), (Q, H), (K, H)]
shuffle(deck) deck
[(7, C), (A, S), (8, S), (9, S), (7, D), (10, D), (Q, C), (2, D), (9, C), (8, C), (2, C), (6, H), (5, C), (7, S), (J, D), (4, C), (4, D), (K, S), (9, H), (10, C), (J, S), (5, H), (A, H), (6, D), (8, H), (3, C), (3, D), (3, H), (Q, H), (4, H), (A, D), (K, C), (5, S), (J, C), (K, H), (5, D), (10, H), (4, S), (J, H), (6, S), (2, H), (Q, D), (3, S), (9, D), (K, D), (6, C), (8, D), (7, H), (Q, S), (10, S), (A, C), (2, S)]
hand = [deck.pop() for card in range(5)] hand
[(2, S), (A, C), (10, S), (Q, S), (7, H)]
deck
[(7, C), (A, S), (8, S), (9, S), (7, D), (10, D), (Q, C), (2, D), (9, C), (8, C), (2, C), (6, H), (5, C), (7, S), (J, D), (4, C), (4, D), (K, S), (9, H), (10, C), (J, S), (5, H), (A, H), (6, D), (8, H), (3, C), (3, D), (3, H), (Q, H), (4, H), (A, D), (K, C), (5, S), (J, C), (K, H), (5, D), (10, H), (4, S), (J, H), (6, S), (2, H), (Q, D), (3, S), (9, D), (K, D), (6, C), (8, D)]