UNIT 9B Randomness in Computa5on: Games with Random Numbers Principles of Compu5ng, Carnegie Mellon University - CORTINA

Similar documents
{ a, b }, { a, c }, { b, c }

Poker Hands. Christopher Hayes

More Probability: Poker Hands and some issues in Counting

GAMBLING ( ) Name: Partners: everyone else in the class

Activity 1: Play comparison games involving fractions, decimals and/or integers.

Venn Diagram Problems

6/24/14. The Poker Manipulation. The Counting Principle. MAFS.912.S-IC.1: Understand and evaluate random processes underlying statistical experiments

Conditional Probability Worksheet

Math 166: Topics in Contemporary Mathematics II

Math-Essentials. Lesson 9-2: Counting Combinations

Poker: Probabilities of the Various Hands

CS Project 1 Fall 2017

Conditional Probability Worksheet

Pan (7:30am) Juan (8:30am) Juan (9:30am) Allison (10:30am) Allison (11:30am) Mike L. (12:30pm) Mike C. (1:30pm) Grant (2:30pm)

3 The multiplication rule/miscellaneous counting problems

CMPSCI 240: Reasoning Under Uncertainty First Midterm Exam

Here are two situations involving chance:

Classical vs. Empirical Probability Activity

Poker: Probabilities of the Various Hands

3 The multiplication rule/miscellaneous counting problems

Intermediate Math Circles November 1, 2017 Probability I

Def: The intersection of A and B is the set of all elements common to both set A and set B

PROBLEM SET 2 Due: Friday, September 28. Reading: CLRS Chapter 5 & Appendix C; CLR Sections 6.1, 6.2, 6.3, & 6.6;

Mathematical Foundations HW 5 By 11:59pm, 12 Dec, 2015

CS Programming Project 1

(a) Suppose you flip a coin and roll a die. Are the events obtain a head and roll a 5 dependent or independent events?

1. An office building contains 27 floors and has 37 offices on each floor. How many offices are in the building?

Counting Poker Hands

2.5 Sample Spaces Having Equally Likely Outcomes

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.


STAT 430/510 Probability Lecture 3: Space and Event; Sample Spaces with Equally Likely Outcomes

LEARN HOW TO PLAY MINI-BRIDGE

Simple Probability. Arthur White. 28th September 2016

Math 1313 Section 6.2 Definition of Probability

The point value of each problem is in the left-hand margin. You must show your work to receive any credit, except on problems 1 & 2. Work neatly.

STATION 1: ROULETTE. Name of Guesser Tally of Wins Tally of Losses # of Wins #1 #2

8.2 Union, Intersection, and Complement of Events; Odds

Inheritance Inheritance

Poker: Further Issues in Probability. Poker I 1/29

4.1 Sample Spaces and Events

Developed by Rashmi Kathuria. She can be reached at

3. If you can t make the sum with your cards, you must draw one card. 4. Players take turns rolling and discarding cards.

TEST A CHAPTER 11, PROBABILITY

Activity 6: Playing Elevens

Dungeon Crawler Card Game

Up & Down GOAL OF THE GAME UP&DOWN CARD A GAME BY JENS MERKL & JEAN-CLAUDE PELLIN ART BY CAMILLE CHAUSSY

CSE231 Spring Updated 04/09/2019 Project 10: Basra - A Fishing Card Game

CSE 312: Foundations of Computing II Quiz Section #1: Counting (solutions)

Unit 9: Probability Assignments

If you roll a die, what is the probability you get a four OR a five? What is the General Education Statistics

This Probability Packet Belongs to:

Probability Review 41

Chapter 2. Permutations and Combinations

The probability set-up

Name: Exam 1. September 14, 2017

4.3 Rules of Probability

Contemporary Mathematics Math 1030 Sample Exam I Chapters Time Limit: 90 Minutes No Scratch Paper Calculator Allowed: Scientific

LISTING THE WAYS. getting a total of 7 spots? possible ways for 2 dice to fall: then you win. But if you roll. 1 q 1 w 1 e 1 r 1 t 1 y

Bridge Players: 4 Type: Trick-Taking Card rank: A K Q J Suit rank: NT (No Trumps) > (Spades) > (Hearts) > (Diamonds) > (Clubs)

MATH 215 DISCRETE MATHEMATICS INSTRUCTOR: P. WENG

27. Important Object- Oriented Programming Ideas

CSE 21: Midterm 1 Solution

Discrete Finite Probability Probability 1

Chapter 4: Introduction to Probability

Project 2 - Blackjack Due 7/1/12 by Midnight

Discrete Random Variables Day 1

CSCI 4150 Introduction to Artificial Intelligence, Fall 2004 Assignment 7 (135 points), out Monday November 22, due Thursday December 9

Section 5.4 Permutations and Combinations

Key Concepts. Theoretical Probability. Terminology. Lesson 11-1

BRIDGE is a card game for four players, who sit down at a

CS 241 Data Organization using C. Project: Identifying the Rank of a Poker Hand and an Empirical Calculation of Probabilities

Section 5.4 Permutations and Combinations

Suppose you are supposed to select and carry out oneof a collection of N tasks, and there are T K different ways to carry out task K.

n(s)=the number of ways an event can occur, assuming all ways are equally likely to occur. p(e) = n(e) n(s)

The probability set-up

Poker Rules Friday Night Poker Club

Chapter 2 Integers. Math 20 Activity Packet Page 1

Page 1 of 22. Website: Mobile:

Intermediate Math Circles November 1, 2017 Probability I. Problem Set Solutions

CARIBBEAN. The Rules

CSE 312: Foundations of Computing II Quiz Section #2: Inclusion-Exclusion, Pigeonhole, Introduction to Probability (solutions)

2. Combinatorics: the systematic study of counting. The Basic Principle of Counting (BPC)

Chapter 1. Probability

Chapter 8: Probability: The Mathematics of Chance

Simulations. 1 The Concept

MC215: MATHEMATICAL REASONING AND DISCRETE STRUCTURES

Simple Poker Game Design, Simulation, and Probability

CS 210 Fundamentals of Programming I Fall 2015 Programming Project 8

Math 227 Elementary Statistics. Bluman 5 th edition

Item Description - MC Phi - Please note: any activity that is not completed during class time may be set for homework or undertaken at a later date.

STANDARD COMPETENCY : 1. To use the statistics rules, the rules of counting, and the characteristic of probability in problem solving.

DELIVERABLES. This assignment is worth 50 points and is due on the crashwhite.polytechnic.org server at 23:59:59 on the date given in class.

C) 1 4. Find the indicated probability. 2) A die with 12 sides is rolled. What is the probability of rolling a number less than 11?

ECE 302 Homework Assignment 2 Solutions

6. In how many different ways can you answer 10 multiple-choice questions if each question has five choices?

Test 2 SOLUTIONS (Chapters 5 7)

The Secret to Performing the Jesse James Card Trick

Important Distributions 7/17/2006

6) A) both; happy B) neither; not happy C) one; happy D) one; not happy

Transcription:

UNIT 9B Randomness in Computa5on: Games with Random Numbers 1 Rolling a die from random import randint def roll(): return randint(0,15110) % 6 + 1 OR def roll(): return randint(1,6) 2 1

Another die def roll():! return randint(0,90)! def roll(): return randint(0,9) * 10! #wrong! #right! 3 Simula5ng a Deck of Cards A deck of cards is made up of 52 cards, where each card has a suit and a rank: Suits: Spades ( ), Hearts ( ), Diamonds ( ), Clubs ( ) Ranks: 2, 3, 4, 5, 6, 7, 8, 9, 10, J (Jack), Q (Queen), K (King), A (Ace) A standard deck of cards has 1 of each combina5on of suit and rank. 4 2

A card deck in Python def create_deck(): deck = [] ranklist = ["2","3","4",...,"K","A"] suitlist = ["clubs",..., "spades"] for suit in range(0,4): for rank in range (0,13): card = [] card.append(ranklist[rank]) card.append(suitlist[suit]) deck.append(card) return deck Do not use... in your code! 5 Rank def get_rank(card): ranklist = ["2",...,"A"] return ranklist.index(card[0]) Do not use... in your code! card rank 2 0 3 1 4 2 5 3 6 4 7 5 8 6 9 7 10 8 J 9 Q 10 K 11 A 12 6 3

Suit def get_suit(card): Do not use... in your code! suitlist = ["clubs",..., "spades"] return suitlist.index(card[1]) card suit (clubs) 0 (diamonds) 1 (hearts) 2 (spades) 3 7 Picking a random card from a deck from random import randint def pick_card(deck): card_number = randint(0,51) return deck[card_number] 8 4

Dealing Random Cards Suppose we have a card game like Poker where we want to be dealt a "hand" of 5 random cards from the deck. What is poten5ally wrong with the following code? hand = [] for i in range(0,5): hand.append(pick_card(deck)) 9 Shuffling the Deck We should shuffle a deck and then create a hand from the first 5 cards in the deck. There are many ways to shuffle a deck of cards. One algorithm: Exchange (swap) the first card with a random card. Exchange the second card with a random card except the first card. Exchange the third card with a random card except the first two cards.... Repeat un5l all cards have been swapped. 10 5

Building the Func5on For the first card (at index 0) in deck d, how do we generate a random index for a card to swap? r = randint(0,len(d)-1) How do we swap the first card with the randomlyselected card? temp = d[0] d[0] = d[r] d[r] = temp or we can use parallel assignment in Python... d[0], d[r] = d[r], d[0] 11 Building the Func5on (cont d) For the second card (at index 1) in deck d, how do we generate a random index for any card except the first card? r = randint(1,len(d)-1) How do we swap the first card with the randomlyselected card? temp = d[1] d[1] = d[r] d[r] = temp or we can use parallel assignment in Python... d[1], d[r] = d[r], d[1] 12 6

Building the Func5on (cont d) For the third card (at index 2) in deck d, how do we generate a random index for any card except the first two cards? r = randint(2,len(d)-1) How do we swap the first card with the randomlyselected card? temp = d[2] d[2] = d[r] d[r] = temp or we can use parallel assignment in Python... d[2], d[r] = d[r], d[2] 13 In general... For the card at index i in deck d, how do we generate a random index for a card to swap? r = randint(i,len(d)-1) How do we swap the first card with the randomlyselected card? temp = d[i] d[i] = d[r] d[r] = temp or we can use parallel assignment in Python... d[i], d[r] = d[r], d[i] 14 7

Shuffling the en5re deck and dealing five cards... def permute(deck): for i in range(0,len(deck)-1): r = randint(i,len(deck)-1) deck[i],deck[r] = deck[r], deck[i] return deck >> hand = permute(deck)[0:5] [ ['10','hearts'],['10','spades'], ['J','spades'],['4','clubs'],['Q','spades'] ] You can also use random.shuffle(deck) to permute! 15 Poker: Detec5ng a Flush In poker, a flush is a hand where all of the cards have the same suit. One possible algorithm: If all of the cards have a suit of spades, return true. If all of the cards have a suit of hearts, return true. If all of the cards have a suit of diamonds, return true. If all of the cards have a suit of clubs, return true. If none of the above tests returns true, return false. 16 8

Poker: Detec5ng a Flush (cont d) def all_spades(hand): for i in range(0,len(hand)): if (hand[i].get_suit()!= 3): return False return True all_hearts, all_diamonds and all_clubs are wriden similarly. card (clubs) 0 (diamonds) 1 (hearts) 2 (spades) 3 suit 17 Poker: Detec5ng a Flush (cont d) def flush(hand): if all_spades(hand): return True if all_hearts(hand): return True if all_diamonds(hand): return True if all_clubs(hand): return True return False equivalent to: if all_spades(hand) == True:! 18 9

Poker: Detec5ng a Flush (Another way) def flush2(hand) for j in range(0,4): # j is suit index count = 0 # reset for suit j for i in range(0,len(hand)): if (hand[i].get_suit() == j): count = count + 1 if count == len(hand): return True return False # all suits were j 19 Simple dice game A player has two die. On each roll, if the player does not roll doubles (same value on each die), then the player wins the sum of the die values. Otherwise, the player earns a strike. The game ends once the player has three strikes. Write a func5on that returns the amount the player wins in a simulated simple dice game. 20 10

Rolling a die def roll(): return randint(1,6) 21 One round of the game die1 = roll() die2 = roll() if die1 == die2: strikes = strikes + 1 else: sum = sum + die1 + die2 22 11

def simple_game(): strikes = 0 Pueng it together sum = 0 while (strikes < 3): die1 = roll() die2 = roll() if die1 == die2: strikes = strikes + 1 else: sum = sum + die1 + die2 return sum 23 What is the average winnings for 1000 players of this game? >>> games = [] >>> for i in range(0,1000):... games.append(simple_game())... >>> games [16, 60, 252, 131, 70,..., 209, 70, 107] >>> total = 0 >>> for score in games:... total = total + score... >> total/1000 => 102.674 24 12