Random. Bart Massey Portland State University Open Source Bridge Conf. June 2014

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

MITOCW MITCMS_608S14_ses03_2

CSE : Python Programming

Pseudorandom Number Generation and Stream Ciphers

Analyzing the Efficiency and Security of Permuted Congruential Number Generators

We all know what it means for something to be random. Or do

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

Random Bit Generation and Stream Ciphers

MITOCW mit_jpal_ses06_en_300k_512kb-mp4

MITOCW R7. Comparison Sort, Counting and Radix Sort

MITOCW watch?v=krzi60lkpek

Theory of Probability - Brett Bernstein

MITOCW R13. Breadth-First Search (BFS)

MITOCW watch?v=fp7usgx_cvm

MITOCW R9. Rolling Hashes, Amortized Analysis

MITOCW Lec 22 MIT 6.042J Mathematics for Computer Science, Fall 2010

ECS 20 (Spring 2013) Phillip Rogaway Lecture 1

2 I'm Mike Institute for Telecommunication Sciences

A4M33PAL, ZS , FEL ČVUT

MITOCW ocw lec11

ECOSYSTEM MODELS. Spatial. Tony Starfield recorded: 2005

MITOCW MITCMS_608S14_ses05

MITOCW watch?v=zkcj6jrhgy8

Comp 3211 Final Project - Poker AI

Determine the Expected value for each die: Red, Blue and Green. Based on your calculations from Question 1, do you think the game is fair?

What now? What earth-shattering truth are you about to utter? Sophocles

Randomized Algorithms

MITOCW R3. Document Distance, Insertion and Merge Sort

MITOCW Lec 25 MIT 6.042J Mathematics for Computer Science, Fall 2010

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

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

Grade 7/8 Math Circles. Visual Group Theory

MITOCW watch?v=-qcpo_dwjk4

Math 1313 Section 6.2 Definition of Probability

MITOCW Recitation 9b: DNA Sequence Matching

MITOCW 8. Hashing with Chaining

Randomness Exercises

MITOCW R11. Principles of Algorithm Design

Math 319 Problem Set #7 Solution 18 April 2002

MITOCW watch?v=tssndp5i6za

the gamedesigninitiative at cornell university Lecture 6 Uncertainty & Risk

Lecture 1. Permutations and combinations, Pascal s triangle, learning to count

3 The multiplication rule/miscellaneous counting problems

Mathematical Magic Tricks

CS Project 1 Fall 2017

MITOCW watch?v=3v5von-onug

MITOCW R22. Dynamic Programming: Dance Dance Revolution

Week 1: Probability models and counting

MITOCW R19. Dynamic Programming: Crazy Eights, Shortest Path

Basics of Five Card Draw

CS256 Applied Theory of Computation

QUICKSTART COURSE - MODULE 7 PART 3

The study of probability is concerned with the likelihood of events occurring. Many situations can be analyzed using a simplified model of probability

PS 3.8 Probability Concepts Permutations & Combinations

In this chord we have the notes F#, C#, and A. You can also look at it as Gb, Db, and A.

More Adversarial Search

MITOCW 7. Counting Sort, Radix Sort, Lower Bounds for Sorting

Due Friday February 17th before noon in the TA drop box, basement, AP&M. HOMEWORK 3 : HAND IN ONLY QUESTIONS: 2, 4, 8, 11, 13, 15, 21, 24, 27

Solution: Alice tosses a coin and conveys the result to Bob. Problem: Alice can choose any result.

The Emperor's New Repository

Mint Tin Mini Skulduggery

Programming Languages and Techniques Homework 3

Lecture 18 - Counting

NFL Strength Coach of the Year talks Combine, Training, Advice for Young Strength Coaches

GOAL CLARITY ROADMAP

BEGINNING BRIDGE Lesson 1

The Teachers Circle Mar. 20, 2012 HOW TO GAMBLE IF YOU MUST (I ll bet you $5 that if you give me $10, I ll give you $20.)

INTRODUCTION TO WEARABLES

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

"Students play games while learning the connection between these games and Game Theory in computer science or Rock-Paper-Scissors and Poker what s

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

Common Phrases (2) Generic Responses Phrases

U strictly dominates D for player A, and L strictly dominates R for player B. This leaves (U, L) as a Strict Dominant Strategy Equilibrium.

MITOCW 15. Single-Source Shortest Paths Problem

BLUFF WITH AI. CS297 Report. Presented to. Dr. Chris Pollett. Department of Computer Science. San Jose State University. In Partial Fulfillment

The Problem. Tom Davis December 19, 2016

Selected Game Examples

Probability with Set Operations. MATH 107: Finite Mathematics University of Louisville. March 17, Complicated Probability, 17th century style

Original Recipe. Square Dance Quilt by Glenn Dragone

If a series of games (on which money has been bet) is interrupted before it can end, what is the fairest way to divide the stakes?

2. The Extensive Form of a Game

SHA532 Transcripts. Transcript: Forecasting Accuracy. Transcript: Meet The Booking Curve

Application: Public Key Cryptography. Public Key Cryptography

Acing Math (One Deck At A Time!): A Collection of Math Games. Table of Contents

Celebration Bar Review, LLC All Rights Reserved

MITOCW Project: Backgammon tutor MIT Multicore Programming Primer, IAP 2007

Things I DON'T Like. Things I DO Like. Skill Quizzes. The Agenda

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

The following content is provided under a Creative Commons license. Your support

Number Theory and Security in the Digital Age

Discrete Mathematics and Probability Theory Spring 2016 Rao and Walrand Note 13

MITOCW watch?v=2g9osrkjuzm

CMS.608 / CMS.864 Game Design Spring 2008

Introduction to Counting and Probability

PLAYERS AGES MINS.

6.00 Introduction to Computer Science and Programming, Fall 2008

How Can I Deal With My Anger?

Venn Diagram Problems

Distributed Settlers of Catan

Anthony Rubbo. Game components. 1 Camp. 30 clocks. 4 dice Each die has the following symbols: 3x food, 2x map and 1x pick & shovel 16 treasure maps

Transcription:

Random Bart Massey <bart.massey@gmail.com> Portland State University Open Source Bridge Conf. June 2014

No Clockwork Universe Stuff doesn't always happen the same even when conditions seem pretty identical. The idea that we could abstract this is really pretty new. Newton: Clockwork universe Fermat, Pascal: Dice calculations Kolmogorov: Axiomatization of probability

A Deterministic Machine A computer is designed to move from state to state deterministically. Failure to do so is usually treated as a bug. Still useful for analyzing randomness. Questions: What do we mean by random? Can we make computers be random? Can we harvest random? Once we have random, what do we do with it?

Let's Play Poker Suppose I want to build a web-based poker server (like everyone else in the world). My server needs to shuffle and deal hands. I need to ensure fair shuffling, or I may be in big trouble. Players need to be sure I've shuffled fairly. Traditional shuffling would be acceptable, but simulating it online is hard It also probably requires randomness

Randomness As Chance Randomness seems kind of about chance and probability. We say that a particular value is uniform random if it is chosen with equal likelihood from all possible values. There's no such thing as a random integer. But there is a random real in a range. This definition is unsatisfying. It is untestable. It can be improved: think random sequences.

Random Sequences As 2s 3s 4s 5s 6s : likely not random (?) As 2h 3s 4h 5s 6h : still likely not random 4s 7s 9s Ks 3s 2s : more random 4s 7h 9h Kc 3d 2h : looks fairly random Information Theory says that random sequences contain maximal information. Kolmogorov: The description of the sequence is as big as the sequence itself.

Randomness as Uncorrelation Another way to think of randomness is as a lack of correlation with other sequences. This is a good definition for us: we want our poker hands to be uncorrelated with anything the players can imagine. Now what we have to do is figure out how to do this.

Shuffling as Permutation First, though, we need to nail down what we mean by a fair shuffle. A fair shuffle will choose any possible permutation of the 52 cards with equal likelihood. (52! 2 226 10 68 choices) Let's assume we have a machine that can generate random integers in the range 1..n for any given n. How do we pick a random permutation?

Problem: Shuffling & Arrays The obvious way to represent cards in our program is as integers in the range 1..52. Then we can represent our deck as an array containing a permutation of cards. We start with a new deck. We want to rearrange the cards such that all permutations are equally likely. Simulate human shuffle: Maybe, but maybe not.

Captain Obvious's Fail Shuffle for i in 1..52 j random integer in 1..52 swap a[i] with a[j] For subtle reason, doesn't work Elements near the front likely get swapped more times than elements near the back. Hard to see by inspection of shuffles. Swapping random pairs doesn't work either. Don't know how many swaps are needed.

Lame Shuffle-Sort What if we attach a random number in the range 1..1000 to each array element as a key and then sort the array? Slow because sort: O(n lg n) time. What about key ties? We would need to retry! Turns out that the bigger the random number, the more expensive our generator will be. Works for some value of work.

Selection Shuffle Repeatedly pick a random card out of the deck and stack it on top of the new deck. Requires randint(52), then randint(51),... Requires counting down to the selected card in our source array, then marking it as used. Can't dodge holes otherwise. So O(n 2 ), not fast at all. Obviously generates all permutations equally. Requires extra destination array.

Selection Shuffle

Selection Shuffle: Hole Plugging Can fix performance problem by hole plugging : always move last card to fill the hole made by removing a card. OK because we only select randomly from the source anyhow. Now the algorithm touches each card at most twice, which is probably as good as we can do.

Hole Plugging

Selection Shuffle: In-place Now each time we select a card, we will leave a hole just below the remaining source. Plug the selected card into the hole. This achieves in-place sorting.

In-Place

Knuth-Morris-Pratt-etc. Shuffle for j in 1..52 k random integer in j..52 exchange a[j] with a[k] Correct, fast, uses little memory Many easy small bugs, so be careful. Many little performance improvements. Now all we need is a way to generate random integers in a range, and we're set.

Pseudo-Random Numbers Remember that computers are deterministic. Maybe we can come up with some way of generating an unpredictable / uncorrelated sequence of random numbers in range 1..m? Can then use the remainder operation to (almost) get the numbers to the range 1..n when n is much smaller than m. These numbers are still not random: Knowing the algorithm in full makes them predictable.

Linear Congruential PRNG (MLCG) s some seed value in 1..m r s rem n s (s a) rem m Choice of a and m matters a lot. Note that s may never be 0. Sample output (a=55, m=251, s=12, n=15): 8, 6, 1, 5, 6, 9, 4, 12, 10, 0, 12 Coefficients from paper: Tables of linear congruential generators of different sizes and good lattice structure., Pierre L'Ecuyer, Math. Comput 01/1999; 68:249-260.

MLCG As A Wheel 8 9 0 1 2 7 3 6 5 4

Implement Me for j in 1..52 k random integer in j..52 exchange a[j] with a[k] r s rem n s (s a) rem m

MLCG Fail Still have to pick a random seed. Often use time of day in ms, but... Still have remainder error. Turns out that these generators can be easily reversed: Can discover s, a andp from very short sequences even given small n. This actually happened, apparently: http://www.cigital.com/papers/download/developer_gambling.php But note that they didn't do it right.

Cryptographic-Secure PRNG Has the property that hidden s may be very difficult to discover from output sequence. Generally pretty expensive per random. Interesting relationship between m and ability to generate all possible poker hands. m must be very large hundreds of bits. Still have to choose initial s.

Terrible Ways To Choose s System clock: predictable. PID: small and predictable. Hash of memory: surprisingly predictable. User timing: Under user control.

Better Ways To Choose s True or likely entropy sources. Hardware RNG: Pick a phenomenon (e.g. thermal noise) that is truly random. Use measurements of that phenomenon. Problems: slow / colored / expensive / fiddly. We're working on it. :-)

Other Uses Of Randomness Different distributions Selection and sampling: Efficient selection tricks Modeling Cryptography

Adversary Games and Randomness Consider Rock-Scissors-Paper. Poor Bart. He always picks rock. John Nash: Sometimes the best strategy is a mixed strategy ; a random choice of moves. In poker, for example, never bluff and always bluff are clearly both fail. What is the best bluffing strategy?

Telephone Poker How can players ensure our server is shuffling and dealing randomly? (In real life, they almost always aren't.) Idea: Arrange things in such a way that nobody has to be trusted. Each side forces randomness on the other. Each side can check at the end. Typically relies on public-key cryptography. (c.f. RSA, GM, etc. in early 1980s)

Paying Attention To Random Bottom line? Random pops up all over the place, and is incredibly important. I could easily do a 10-week course. It is easy to get wrong, with sometimes dire consequences. Get it right, and you'll have a decent poker server that people will actually want to play on.