Open In Colab

Welcome to the crapssim sandbox!

This is a quick attempt to allow people to run the crapssim Python package without requiring a detailed python installation. Some sample code is set up to get you started on running craps simulations quickly.

Setup

!pip install crapssim
import crapssim as craps
from crapssim.strategy import *
from crapssim.strategy.examples import PlaceInside, IronCross, HammerLock, Risk12, Knockout, DiceDoctor

import numpy as np
import pandas as pd
from plotnine import *
Defaulting to user installation because normal site-packages is not writeable
Collecting crapssim
  Downloading crapssim-0.3.0-py3-none-any.whl (72 kB)
?25l     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.0/72.5 KB ? eta -:--:--
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 72.5/72.5 KB 2.6 MB/s eta 0:00:00
?25h
Collecting numpy>=1.18.0
  Downloading numpy-2.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.4 MB)
?25l     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.0/16.4 MB ? eta -:--:--
     ╸━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.3/16.4 MB 9.9 MB/s eta 0:00:02
     ━━━━╺━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/16.4 MB 26.2 MB/s eta 0:00:01
     ━━━━━━━━━━━━━━━╸━━━━━━━━━━━━━━━━━━━━━━━━ 6.5/16.4 MB 63.4 MB/s eta 0:00:01
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━╸━━━━━━━━━━━ 11.6/16.4 MB 132.7 MB/s eta 0:00:01
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸ 16.4/16.4 MB 154.7 MB/s eta 0:00:01
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸ 16.4/16.4 MB 154.7 MB/s eta 0:00:01
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 16.4/16.4 MB 84.0 MB/s eta 0:00:00
?25h
Installing collected packages: numpy, crapssim
Successfully installed crapssim-0.3.0 numpy-2.2.1
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 7
      4 from crapssim.strategy.examples import PlaceInside, IronCross, HammerLock, Risk12, Knockout, DiceDoctor
      6 import numpy as np
----> 7 import pandas as pd
      8 from plotnine import *

ModuleNotFoundError: No module named 'pandas'

Pick your favorite strategy

Either select from the list of available strategies, or write your own. The file strategy/examples.py has examples of full strategies, but you can check out the detailed tutorial for making your own.

#@title  { run: "auto", display-mode: "form" }
mode = "write strategy" #@param ["pick strategy", "write strategy"]

if mode == 'pick strategy':

  strategy = DiceDoctor(10) # @param ["BetPassLine(10)","BetDontPass(10)","BetPassLine(10) + PassLineOddsMultiplier(2)","PlaceInside(10)","BetPlace({6: 12, 8: 12}, skip_point=False)","IronCross(10)","HammerLock(10)","Risk12()","Knockout(10)","DiceDoctor(10)"] {"type":"raw","allow-input":true}

elif mode == 'write strategy':

  print("Write your strategy in code above (click 'show code'):")

  ## Write your strategy below
  strategy = BetPassLine(10) + PassLineOddsMultiplier(odds_multiplier=2) + BetPlace({6: 12, 8: 12})
Write your strategy in code above (click 'show code'):

Test your strategy

Make sure everything is working right, especially if you write a custom strategy. (This is usually the time consuming process for me.) You can adjust the number of rolls and bankroll accordingly.

# Adjust bankroll or rolls here
bankroll = 200
max_rolls = 20

table = craps.Table()
table.add_player(bankroll, strategy)
table.run(max_rolls=max_rolls)
Welcome to the Craps Table!
Initial players: ['Player 0']

Dice out!
Shooter rolled 5 (4, 1)
Point is On (5)
Total Player Cash is $200.0

Dice out!
Shooter rolled 6 (5, 1)
Player 0 won $14.0 on Place(6, amount=12.0)!
Point is On (5)
Total Player Cash is $214.0

Dice out!
Shooter rolled 5 (1, 4)
Point is Off (None)
Total Player Cash is $214.0

Dice out!
Shooter rolled 9 (3, 6)
Point is On (9)
Total Player Cash is $214.0

Dice out!
Shooter rolled 9 (4, 5)
Point is Off (None)
Total Player Cash is $214.0

Dice out!
Shooter rolled 9 (5, 4)
Point is On (9)
Total Player Cash is $214.0

Dice out!
Shooter rolled 11 (5, 6)
Point is On (9)
Total Player Cash is $214.0

Dice out!
Shooter rolled 9 (3, 6)
Point is Off (None)
Total Player Cash is $214.0

Dice out!
Shooter rolled 4 (2, 2)
Point is On (4)
Total Player Cash is $214.0

Dice out!
Shooter rolled 7 (6, 1)
Player 0 lost $12.0 on Place(8, amount=12.0).
Player 0 lost $12.0 on Place(6, amount=12.0).
Point is Off (None)
Total Player Cash is $190.0

Dice out!
Shooter rolled 7 (3, 4)
Point is Off (None)
Total Player Cash is $190.0

Dice out!
Shooter rolled 5 (4, 1)
Point is On (5)
Total Player Cash is $190.0

Dice out!
Shooter rolled 3 (1, 2)
Point is On (5)
Total Player Cash is $190.0

Dice out!
Shooter rolled 7 (6, 1)
Player 0 lost $12.0 on Place(6, amount=12.0).
Player 0 lost $12.0 on Place(8, amount=12.0).
Point is Off (None)
Total Player Cash is $166.0

Dice out!
Shooter rolled 8 (2, 6)
Point is On (8)
Total Player Cash is $166.0

Dice out!
Shooter rolled 10 (5, 5)
Point is On (8)
Total Player Cash is $166.0

Dice out!
Shooter rolled 7 (1, 6)
Player 0 lost $12.0 on Place(6, amount=12.0).
Player 0 lost $12.0 on Place(8, amount=12.0).
Point is Off (None)
Total Player Cash is $142.0

Dice out!
Shooter rolled 7 (5, 2)
Point is Off (None)
Total Player Cash is $142.0

Dice out!
Shooter rolled 8 (4, 4)
Point is On (8)
Total Player Cash is $142.0

Dice out!
Shooter rolled 4 (2, 2)
Point is On (8)
Total Player Cash is $142.0

Simulate many sessions

Use this code to simulate your chosen strategy many times and get a sense for what you might expect at the craps table. For a reliable outcome, I recommend at least 100,000 simulations (which will take a few minutes)

#@title Simulation parameters
n_sim = 10000 #@param {type:"integer"}
bankroll = 1000 #@param {type:"number"}
max_rolls =  144 #@param {type:"raw"}
max_shooter =  float("inf") #@param {type:"raw"}

print_every = 1000 #@param {type:"integer"}
results = []

print("[sim, final, initial, n_rolls]")
for i in range(n_sim):
  table = craps.Table()
  table.add_player(bankroll, strategy, 1)
  table.run(max_rolls, max_shooter, verbose=False)


  row = [i, table.players[0].bankroll, bankroll, table.dice.n_rolls]
  if (i % print_every == 0):
    print(row)
  results.append(row)

# Create dataframe of results
results = pd.DataFrame(results, columns = ['sim', 'final', 'initial', 'n_rolls'])
results['winnings'] = results['final'] - results['initial']
[sim, final, initial, n_rolls]
[0, 1030.0, 1000, 144]
[1000, 890.0, 1000, 144]
[2000, 960.0, 1000, 144]
[3000, 970.0, 1000, 144]
[4000, 1040.0, 1000, 144]
[5000, 970.0, 1000, 144]
[6000, 1060.0, 1000, 144]
[7000, 960.0, 1000, 144]
[8000, 930.0, 1000, 144]
[9000, 1040.0, 1000, 144]

Visualize results

# Density plot
(ggplot(results, aes(x='winnings')) +
 geom_vline(xintercept = 0, color = 'grey') +
 geom_density() +
 theme_minimal() +
 labs(x = "Winnings", y = "Relative chance of outcome")
)
# Boxplot
(ggplot(results,aes(y='winnings',x=0)) +
 geom_hline(yintercept = 0, color = 'grey') +
 geom_boxplot(width = 0.4) +
 xlim(-1, 1) +
 coord_flip() +
 theme_minimal()
)