***Poker Probability and Calculator Demo Example*** ---- This page is under development. Comments are welcome, but please load any comments in the comments section at the bottom of the page. Please include your wiki MONIKER in your comment with the same courtesy that I will give you. Its very hard to reply intelligibly without some background of the correspondent. Thanks,[gold] ---- # [gold] Here is some starter code for poker probability. The impetus for this calculator was checking some poker probabilities of 5 card draw in a book. Its easy enough to plug in formulas into the calculating subroutine. Most of the checked testcases involve 5 card poker. ---- # In planning any software, it is advisable to gather a number of testcases to check the results of the program. The math for the testcases can be checked by pasting statements in the TCL console. Also, some of the pseudocode statements were checked in the google search engine which will take math expressions. The factorial procedure used below was posted by Suchenworth. Aside from the TCL calculator display, when one presses the report button on the calculator, one will have console show access to the factorial function. ---- ***Testcases Section*** * Testcase 1 # For a system of dealing 5 cards from a 52 card deck, the sample space would be factorial(52)/((factorial(5)*factorial(52-5)) or 52!/(5!*47!). Most of these TCL expressions can be pasted into the console window. ====== set space [ expr { [f 52]/[f 5]/[f 47]} ] ;# generic TCL answer% 2598960 ====== # The probability of four of a kind would be frequency/ sample space: ====== set probability [ expr { (1.* 4 )/2598960} ] answer% 1.5391E-6 ====== # The probability of four of a kind would be frequency/ sample space: ====== set probability [ expr { (1.* 624 )/2598960} ] answer% 2.40E-4 ====== # The probability of three of a kind would be frequency/ sample space: ====== set probability [ expr { (1.* 54912 )/2598960} ] answer% 0.021 ====== * Testcase 2 # Continuing with dealing 5 cards from a 52 card deck, there is an alternate way of estimating the factorial, which is the Swerling formula. The Swerling formula is less accurate for small numbers (<60!), but has some interest in estimating the factorial with large numbers (>60!). The Swerling formula is sometimes used to check the magnitude of large N! in computing. The Swerling formula is implemented and accessible from the report console window. ====== set swerlingspace [ expr { [fact 52]/[fact 5]/[fact 47]} ] answer% 2599403 # and the answers for probabilites would slightly different set probability [ expr { (1.* 4 )/2599403 } ] answer% 1.53881E-6 vis previous 1.5391E-6 ====== ---- ---- ***Screenshots Section*** figure 1. [http://imageshack.us/a/img24/835/kez.gif] ---- ---- ***References:*** * Robson, Eleanor, Mesopotamian Mathematics, 2100-1600BCE,Oxford 1999 ---- ****Appendix Code**** ****appendix TCL programs and scripts **** ---- ====== # pretty print from autoindent and ased editor # poker probability calculator # written on Windows XP on eTCL # working under TCL version 8.5.6 and eTCL 1.0.1 # gold on TCL WIKI , 12jul2013 package require Tk frame .frame -relief flat -bg aquamarine4 pack .frame -side top -fill y -anchor center set names {{} {number of cards in deck:} } lappend names {distinct hands:} lappend names {frequency: } lappend names {sample space:} lappend names {probability:} lappend names {odds:1} foreach i {1 2 3 4 5 6} { label .frame.label$i -text [lindex $names $i] -anchor e entry .frame.entry$i -width 35 -textvariable side$i grid .frame.label$i .frame.entry$i -sticky ew -pady 2 -padx 1 } proc about {} { set msg "Calculator for Poker Probability from TCL WIKI, written on eTCL " tk_messageBox -title "About" -message $msg } proc f n { expr {$n < 2 ? 1 : $n * [f [incr n -1]]} } proc fact n {expr { $n<2? 1: $n>20? pow($n,$n)*exp(-$n)*sqrt(2*acos(-1)*$n): wide($n)*[fact [incr n -1]]}} proc calculate { } { global answer2 global side1 side2 side3 side4 side5 global side6 set aa [ expr { int($side1) } ] set bb [ expr { int($side2) } ] set cc [ expr { $aa-$bb } ] set cc [ expr { int($cc) } ] set dd [ expr { int($side3) } ] set term1 [ f $aa ] set term2 [ f $bb ] set term3 [ f $cc ] set samplespace [ expr {$term1*1./(1.*$term2*$term3)} ] set pokerprob [ expr { (1.*$dd)/$samplespace } ] set pokerodds [ expr { 1./$pokerprob } ] set side4 $samplespace set side5 $pokerprob set side6 $pokerodds } proc fillup {aa bb cc dd ee ff} { .frame.entry1 insert 0 "$aa" .frame.entry2 insert 0 "$bb" .frame.entry3 insert 0 "$cc" .frame.entry4 insert 0 "$dd" .frame.entry5 insert 0 "$ee" .frame.entry6 insert 0 "$ff" } proc clearx {} { foreach i {1 2 3 4 5 6 } { .frame.entry$i delete 0 end } } proc reportx {} { global side1 side2 side3 side4 side5 global side6 console show; puts "number of cards: $side1 " puts "distinct hards: $side2 " puts "frequency: $side3 " puts "answer sample space: $side4 " puts "hand probability: $side5 " puts "integer odds:1 $side6 " } frame .buttons -bg aquamarine4 ::ttk::button .calculator -text "Solve" -command { calculate } ::ttk::button .test2 -text "Testcase1" -command {clearx;fillup 52. 5. 4. 2.59E6 1.5E-6 6.49E5 } ::ttk::button .test3 -text "Testcase2" -command {clearx;fillup 52. 5. 624. 8. 2.4E-4 4164 } ::ttk::button .test4 -text "Testcase3" -command {clearx;fillup 52. 5. 54912. 4096. 2.11E-2 47.3 } ::ttk::button .clearallx -text clear -command {clearx } ::ttk::button .about -text about -command about ::ttk::button .cons -text report -command { reportx } ::ttk::button .exit -text exit -command {exit} pack .calculator -in .buttons -side top -padx 10 -pady 5 pack .clearallx .cons .about .exit .test4 .test3 .test2 -side bottom -in .buttons grid .frame .buttons -sticky ns -pady {0 10} . configure -background aquamarine4 -highlightcolor brown -relief raised -border 30 wm title . "Poker Probability Calculator " ====== ---- # For the push buttons, the recommended procedure is push testcase and fill frame, change first three entries etc, push solve, and then push report. Report allows copy and paste from console, but takes away from computer "efficiency". While the testcases are in units of cards, the units cancel out in the calculator equations. ---- '''Comments Section''' Please place any comments here, Thanks. ---- <> Numerical Analysis | Toys | Calculator | Example | Package