Look what I found!! This is an archive of what my site/life was about 4-5 years ago. For the modern JobyBednar.com, try here.
JobyBednar.com
Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.
- Robert Heinlein

Ruby Development
/root
  ./apple I
  ./articles
  ./code
  ./decode
  ./hobbies
  ./mac
  ./pics
  ./ruby
  ./www



 Use OpenOffice.org
craps.rb

1 #Craps Simulator - (c)2005 Joby Bednar, All Rights Reserved 2 #---------------------------------------------------------- 3 4 #Inits 5 montewins = 0 6 mcruns = 0 7 mcrolls = 0 8 h = Hash.new(0) 9 10 rand 11 srand(rand(srand)) 12 puts "---seed: %d---"%srand 13 14 #------------------------------------------------------ 15 #Simulation settings 16 montecarlo = 10000 #number of monte carlo runs (change verbose to "no" if montecarlo > 1) 17 verbose = "no" #yes | no (logging of each roll) 18 mcstartcash = 400 #beginning cash to play with 19 #------------------------------------------------------ 20 21 puts "--------SUMMARY----------" 22 23 1.upto(montecarlo) do |mc| 24 25 #Init variables 26 27 hways = Hash.new(0) 28 hwayspost = Hash.new(0) 29 hpost = Hash.new(0) 30 rolls = 0 31 runroll = 0 32 runrollcount = 0 33 point = 0 34 cashhigh = 0 35 cashlow = 1000000 36 gamestate = "off" 37 total = 0 38 dice = Array.new() 39 hardcount = 0 40 runs = 0 41 postcount = 0 42 pointmade = 0 43 pointmissed = 0 44 comingout711 = 0 45 comingout12 = 0 46 comingout23 = 0 47 pass_wins = 0 48 pass_loss = 0 49 nopass_wins = 0 50 nopass_loss = 0 51 odds_wins = 0 52 odds_loss = 0 53 hw_wins = 0 54 hw_loss = 0 55 updowncount = 0 56 #bet type inits 57 pass_bet = 0 58 nopass_bet = 0 59 odds_bet = 0 60 hw_bet = 0 61 #set random seeds 62 #rand 63 #srand(rand(srand)) 64 65 #------------------------------------------------------ 66 #Simulation settings 67 startcash = mcstartcash #beginning cash to play with 68 betsize = 10 # $ amount of unit bet 69 numruns = 1 #simulate up to this number of runs or cash = 0 (use 1 for monte carlo) 70 walkaway = [200,600] #array of low and high bounds for when to walk away 71 runsize = 3 #log any run greater than this number of consecutive rolls (-1 to log all) 72 strategy = "nopass" #pass | nopass 73 playodds = "yes" #yes | no 74 playhw = "no" #yes | no (place a hardways bet if point is 4,6,8,10) 75 odds_run = 0 #place the odds bet n rolls into run, 0 = after point, 1 = after 1 roll, etc. 76 xodds = 1 #when taking odds, n times odds 77 changestrat_losses = -1 #change strategy at x losses, use -1 to not use a change strategy 78 #------------------------------------------------------ 79 80 if verbose=="yes" then puts "------%d------"%mc end 81 82 #Game play 83 cash = startcash 84 until (cash <= walkaway[0] or cash >= walkaway[1] or runs >= numruns) 85 #betting strategies 86 if updowncount == changestrat_losses 87 if strategy=="pass" 88 strategy="nopass" 89 elsif strategy=="nopass" 90 strategy="pass" 91 end 92 updowncount = 0 93 end 94 if verbose=="yes" then puts "strategy: "+strategy end 95 if strategy == "pass" 96 if (pass_bet == 0 and gamestate == "off") 97 pass_bet = betsize 98 cash -= betsize 99 end 100 elsif strategy == "nopass" 101 if (nopass_bet == 0 and gamestate == "off") 102 nopass_bet = betsize 103 cash -= betsize 104 end 105 end 106 if playodds == "yes" and gamestate == "on" and odds_bet == 0 and odds_run == runroll 107 if strategy == "pass" 108 odds_bet = xodds*betsize 109 cash -= odds_bet 110 elsif strategy == "nopass" 111 odds_bet = case point 112 when 6,8: (betsize*xodds*6.0/5.0).floor 113 when 5,9: (betsize*xodds*3.0/2.0).floor 114 when 4,10: (betsize*xodds*2.0/1.0).floor 115 end 116 cash -= odds_bet 117 end 118 end 119 if playhw == "yes" and gamestate == "on" and hw_bet == 0 120 if [4,6,8,10].include?(point) 121 hw_bet = betsize*0.5 122 cash -= betsize*0.5 123 end 124 end 125 if verbose=="yes" then puts "line bet: "+(nopass_bet+pass_bet).to_s end 126 if verbose=="yes" and playodds=="yes" then puts "odds bet: "+odds_bet.to_s end 127 if verbose=="yes" and playhw=="yes" then puts "hw bet: "+hw_bet.to_s end 128 129 #roll dice 130 dice.replace([rand(6)+1,rand(6)+1]) 131 total = dice[0].to_i + dice[1].to_i 132 if gamestate=="on" and not total==7 and not total==point 133 runroll += 1 134 end 135 if gamestate=="off" 136 if [7,11].include?(total.to_i) 137 comingout711 += 1 138 elsif [2,3].include?(total.to_i) 139 comingout23 += 1 140 elsif total == 12 141 comingout12 += 1 142 end 143 end 144 if verbose=="yes" then puts "roll:"+total.to_s end 145 if dice[0]==dice[1] and [2,3,4,5].include?(dice[0]) 146 if gamestate == "on" and not point==0 147 if verbose=="yes" then puts "(%d hardway)"%(dice[0]+dice[1]) end 148 hardcount += 1 149 if runroll > runsize 150 hwayspost[total] += 1 151 else 152 hways[total] += 1 153 end 154 end 155 end 156 157 #settle payoffs 158 if strategy == "pass" 159 160 if gamestate == "on" 161 if total.to_i == point.to_i 162 #pay pass 163 pass_wins += pass_bet 164 cash += pass_bet*2.0 165 pass_bet = 0 166 #pay odds 167 winnings = case point 168 when 6,8: (odds_bet*6.0/5.0).floor 169 when 5,9: (odds_bet*3.0/2.0).floor 170 when 4,10: (odds_bet*2.0/1.0).floor 171 end 172 odds_wins += winnings 173 cash += (winnings + odds_bet) 174 odds_bet = 0 175 updowncount = 0 176 #pay hw 177 if dice[0]==dice[1] 178 winnings = case point 179 when 4,10: (hw_bet*9.0/1.0).floor 180 when 6,8: (hw_bet*7.0/1.0).floor 181 else 0 182 end 183 hw_wins += winnings 184 cash += winnings + hw_bet 185 hw_bet = 0 186 if verbose=="yes" then puts "[HW Paid]" end 187 end 188 if verbose=="yes" then puts "***************[Paid]" end 189 end 190 if total.to_i == 7 191 #lose pass 192 pass_loss += pass_bet 193 pass_bet = 0 194 #lose odds 195 odds_loss += odds_bet 196 odds_bet = 0 197 updowncount += 1 198 if verbose=="yes" then puts "***************[Lost]" end 199 end 200 else #state = "off" 201 if [7,11].include?(total.to_i) 202 #pay pass 203 pass_wins += pass_bet 204 cash += pass_bet*2.0 205 pass_bet = 0 206 updowncount = 0 207 odds_bet = 0 208 if verbose=="yes" then puts "***************[Paid]" end 209 end 210 if[2,3,12].include?(total.to_i) 211 #lose pass 212 pass_loss += pass_bet 213 pass_bet = 0 214 updowncount += 1 215 odds_bet = 0 216 if verbose=="yes" then puts "***************[Lost]" end 217 end 218 end 219 220 elsif strategy == "nopass" 221 222 if gamestate == "on" 223 if total.to_i == 7 224 #pay nopass 225 nopass_wins += nopass_bet 226 cash += nopass_bet*2.0 227 nopass_bet = 0 228 #pay odds 229 winnings = case point 230 when 6, 8 : (odds_bet*5.0/6.0).floor 231 when 5, 9 : (odds_bet*2.0/3.0).floor 232 when 4, 10 : (odds_bet*1.0/2.0).floor 233 end 234 odds_wins += winnings 235 cash += winnings+odds_bet 236 odds_bet = 0 237 updowncount = 0 238 if verbose=="yes" then puts "***************[Paid]" end 239 end 240 if total.to_i == point.to_i 241 #lose nopass 242 nopass_loss += nopass_bet 243 nopass_bet = 0 244 #lose odds 245 odds_loss += odds_bet 246 odds_bet = 0 247 updowncount += 1 248 #pay hw 249 if dice[0]==dice[1] 250 winnings = case point 251 when 4,10: (hw_bet*9.0/1.0).floor 252 when 6,8: (hw_bet*7.0/1.0).floor 253 else 0 254 end 255 hw_wins += winnings 256 cash += winnings + hw_bet 257 hw_bet = 0 258 if verbose=="yes" then puts "[HW Paid]" end 259 end 260 if verbose=="yes" then puts "***************[Lost]" end 261 end 262 else #state = "off" 263 if [2,3].include?(total.to_i) 264 #pay nopass 265 nopass_wins += nopass_bet 266 cash += nopass_bet*2.0 267 nopass_bet = 0 268 updowncount = 0 269 odds_bet = 0 270 if verbose=="yes" then puts "***************[Paid]" end 271 end 272 if [7,11].include?(total.to_i) 273 #lose nopass 274 nopass_loss += nopass_bet 275 nopass_bet = 0 276 updowncount += 1 277 odds_bet = 0 278 if verbose=="yes" then puts "***************[Lost]" end 279 end 280 end 281 282 end 283 284 #game state 285 if (gamestate == "off" and [4,5,6,8,9,10].to_a.include?(total.to_i)) 286 point = total.to_i 287 gamestate.replace("on") 288 if verbose=="yes" then puts "[point on]" end 289 elsif (gamestate == "on" and [7,point.to_i].to_a.include?(total.to_i)) 290 if total == 7 291 pointmissed += 1 292 else 293 pointmade += 1 294 end 295 point = 0 296 runs += 1 297 if runroll > runsize 298 postcount += 1 299 hpost[runroll] += 1 300 end 301 runrollcount += runroll 302 runroll = 0 303 gamestate.replace("off") 304 hw_loss += hw_bet 305 hw_bet = 0 306 if verbose=="yes" then puts "[point off]" end 307 end 308 if verbose=="yes" and not point==0 then puts "point:"+point.to_s end 309 310 #logging 311 h[total] += 1 312 rolls += 1 313 if cashhigh < cash 314 cashhigh = cash.to_i 315 end 316 if cashlow > cash 317 cashlow = cash.to_i 318 end 319 if verbose=="yes" then puts "--Cash: "+cash.to_s+"--" end 320 end 321 322 #report results 323 if montecarlo==1 324 puts "--------TOTALS----------" 325 puts "seed: "+srand.to_s 326 puts "---------" 327 puts "pass wins: $%1.2f"%(pass_wins-pass_loss) 328 puts "nopass wins: $%1.2f"%(nopass_wins-nopass_loss) 329 puts "odds wins: $%1.2f"%(odds_wins-odds_loss) 330 puts "hardways wins: $%1.2f"%(hw_wins-hw_loss) 331 puts "TOTAL WINS: $%1.2f"%(cash-startcash) 332 puts "---------" 333 puts "start: $%1.2f"%mcstartcash 334 puts "cash: $%1.2f"%cash 335 puts "high: $%1.2f"%cashhigh 336 puts "low: $%1.2f"%cashlow 337 puts "pass wins: $%1.2f"%pass_wins 338 puts "pass loss: $%1.2f"%pass_loss 339 puts "nopass wins: $%1.2f"%nopass_wins 340 puts "nopass loss: $%1.2f"%nopass_loss 341 puts "odds wins: $%1.2f"%odds_wins 342 puts "odds loss: $%1.2f"%odds_loss 343 puts "hardways wins: $%1.2f"%hw_wins 344 puts "hardways loss: $%1.2f"%hw_loss 345 puts "---------" 346 puts "rolls: "+rolls.to_s 347 print "runs: "+runs.to_s+" (%2.2f"%(runrollcount/runs.to_f), 348 " rolls/run before 7 or point)\n" 349 print "point made: "+pointmade.to_s+"\t[", 350 ("%2.2f"%(pointmade*100.0/runs.to_f))+"% (40.61%)]\n" 351 print "point miss: "+pointmissed.to_s+"\t[", 352 ("%2.2f"%(pointmissed*100.0/runs.to_f))+"% (59.39%)]\n" 353 print "7|11 on out: "+comingout711.to_s+"\t[", 354 ("%2.2f"%(comingout711*100.0/rolls.to_f))+"% (8/36 = "+("%2.2f"%(8/0.360))+"%)]\n" 355 print "2|3 on out: "+comingout23.to_s+"\t[", 356 ("%2.2f"%(comingout23*100.0/rolls.to_f))+"% (3/36 = "+("%2.2f"%(3/0.360))+"%)]\n" 357 print "12 on out: "+comingout12.to_s+"\t[", 358 ("%2.2f"%(comingout12*100.0/rolls.to_f))+"% (1/36 = "+("%2.2f"%(1/0.360))+"%)]\n" 359 puts "---------" 360 print "runs after "+runsize.to_s+" rolls: ", 361 postcount.to_s+(" (%2.2f%%)"%(postcount*100/runs.to_f)),"\n" 362 hapost = hpost.sort 363 hapost.each {|key| puts key[0].to_s+" rolls: "+key[1].to_s+" (%2.2f%%)"%(key[1]*100/runs.to_f)} 364 puts "---------" 365 puts "hardways: "+hardcount.to_s 366 haways = hways.sort 367 haways.each {|key| puts key[0].to_s+": "+key[1].to_s} 368 puts "--after "+runsize.to_s+" runs--" 369 hawayspost = hwayspost.sort 370 hawayspost.each {|key| puts key[0].to_s+": "+key[1].to_s} 371 puts "------------------" 372 2.upto(12) do |key| 373 ways = case key 374 when 2..7: key-1 375 else 13-key 376 end 377 print "%2d"%key+": "+"%3d"%h[key]+"\t", 378 "["+("%2.2f"%(h[key]*100.0/rolls.to_f))+"% (", 379 ways.to_s+"/36 = "+("%2.2f"%(ways/0.360))+"%)]\n" 380 end 381 382 else 383 montewins += cash-mcstartcash 384 mcruns += runs.to_i 385 mcrolls += rolls.to_i 386 #puts "WINS: $%1.2f (seed: "%(cash-mcstartcash)+srand.to_s+")" 387 if mc == montecarlo then 388 puts "MCs: "+mc.to_s 389 puts "Runs: "+mcruns.to_s 390 puts "Rolls: "+mcrolls.to_s 391 end 392 end 393 394 end #monte carlo 395 396 if montecarlo > 1 397 puts "------------------" 398 puts "TOTAL WINS: $%1.2f"%(montewins) 399 av_wins = montewins/montecarlo.to_f 400 puts "AVG WINS PER GAME: $%1.2f"%av_wins 401 puts "YOUR EDGE: %1.3f%%"%(av_wins*100.0/mcstartcash.to_f) 402 puts "------------------" 403 2.upto(12) do |key| 404 ways = case key 405 when 2..7: key-1 406 else 13-key 407 end 408 print "%2d"%key+": "+"%3d"%h[key]+"\t", 409 "["+("%2.2f"%(h[key]*100.0/mcrolls.to_f))+"% (", 410 ways.to_s+"/36 = "+("%2.2f"%(ways/0.360))+"%)]\n" 411 end 412 end
Sample Output (single game)
USING:
#------------------------------------------------------
#Simulation settings
montecarlo = 1 #number of monte carlo runs (change verbose to "no" if montecarlo > 1)
verbose = "no" #yes | no (logging of each roll)
mcstartcash = 400 #beginning cash to play with
#------------------------------------------------------
#Simulation settings
startcash = mcstartcash #beginning cash to play with
betsize = 10 # $ amount of unit bet
numruns = 100 #simulate up to this number of runs or cash = 0 (use 1 for monte carlo)
walkaway = [200,600] #array of low and high bounds for when to walk away
runsize = 3 #log any run greater than this number of consecutive rolls (-1 to log all)
strategy = "nopass" #pass | nopass
playodds = "yes" #yes | no
playhw = "no" #yes | no (place a hardways bet if point is 4,6,8,10)
odds_run = 0 #place the odds bet n rolls into run, 0 = after point, 1 = after 1 roll, etc.
xodds = 1 #when taking odds, n times odds
changestrat_losses = -1 #change strategy at x losses, use -1 to not use a change strategy
#------------------------------------------------------

OUTPUT:
---seed: 69196414---
--------SUMMARY----------
--------TOTALS----------
seed: 1117892168
---------
pass wins: $0.00
nopass wins: $120.00
odds wins: $94.00
hardways wins: $0.00
TOTAL WINS: $214.00
---------
start: $400.00
cash: $614.00
high: $614.00
low: $360.00
pass wins: $0.00
pass loss: $0.00
nopass wins: $290.00
nopass loss: $170.00
odds wins: $250.00
odds loss: $156.00
hardways wins: $0.00
hardways loss: $0.00
---------
rolls: 205
runs: 35 (3.46 rolls/run before 7 or point)
point made: 10	[28.57% (40.61%)]
point miss: 25	[71.43% (59.39%)]
7|11 on out: 7	[3.41% (8/36 = 22.22%)]
2|3 on out: 4	[1.95% (3/36 = 8.33%)]
12 on out: 3	[1.46% (1/36 = 2.78%)]
---------
runs after 3 rolls: 12 (34.29%)
4 rolls: 5 (14.29%)
5 rolls: 1 (2.86%)
6 rolls: 2 (5.71%)
7 rolls: 1 (2.86%)
8 rolls: 1 (2.86%)
11 rolls: 1 (2.86%)
15 rolls: 1 (2.86%)
---------
hardways: 16
4: 2
6: 5
10: 3
--after 3 runs--
6: 2
10: 4
------------------
 2:   7	[3.41% (1/36 = 2.78%)]
 3:  11	[5.37% (2/36 = 5.56%)]
 4:  17	[8.29% (3/36 = 8.33%)]
 5:  22	[10.73% (4/36 = 11.11%)]
 6:  34	[16.59% (5/36 = 13.89%)]
 7:  31	[15.12% (6/36 = 16.67%)]
 8:  23	[11.22% (5/36 = 13.89%)]
 9:  17	[8.29% (4/36 = 11.11%)]
10:  25	[12.20% (3/36 = 8.33%)]
11:  11	[5.37% (2/36 = 5.56%)]
12:   7	[3.41% (1/36 = 2.78%)]
Sample Output (montecarlo)
USING:
#------------------------------------------------------
#Simulation settings
montecarlo = 10000 #number of monte carlo runs (change verbose to "no" if montecarlo > 1)
verbose = "no" #yes | no (logging of each roll)
mcstartcash = 400 #beginning cash to play with
#------------------------------------------------------
#Simulation settings
startcash = mcstartcash #beginning cash to play with
betsize = 10 # $ amount of unit bet
numruns = 1 #simulate up to this number of runs or cash = 0 (use 1 for monte carlo)
walkaway = [200,600] #array of low and high bounds for when to walk away
runsize = 3 #log any run greater than this number of consecutive rolls (-1 to log all)
strategy = "nopass" #pass | nopass
playodds = "yes" #yes | no
playhw = "no" #yes | no (place a hardways bet if point is 4,6,8,10)
odds_run = 0 #place the odds bet n rolls into run, 0 = after point, 1 = after 1 roll, etc.
xodds = 1 #when taking odds, n times odds
changestrat_losses = -1 #change strategy at x losses, use -1 to not use a change strategy
#------------------------------------------------------

OUTPUT:
---seed: 680139467---
--------SUMMARY----------
MCs: 10000
Runs: 10000
Rolls: 50654
------------------
TOTAL WINS: $-1359.00
AVG WINS PER GAME: $-0.14
YOUR EDGE: -0.034%
------------------
 2: 1336	[2.64% (1/36 = 2.78%)]
 3: 2789	[5.51% (2/36 = 5.56%)]
 4: 4256	[8.40% (3/36 = 8.33%)]
 5: 5618	[11.09% (4/36 = 11.11%)]
 6: 6971	[13.76% (5/36 = 13.89%)]
 7: 8445	[16.67% (6/36 = 16.67%)]
 8: 7053	[13.92% (5/36 = 13.89%)]
 9: 5721	[11.29% (4/36 = 11.11%)]
10: 4206	[8.30% (3/36 = 8.33%)]
11: 2797	[5.52% (2/36 = 5.56%)]
12: 1462	[2.89% (1/36 = 2.78%)]