Inspired by the video from Scam Nation and James Grime from Numberphile, I tried to make a Razzle Dazzle simulator.
Razzle Dazzle is a scam in the form of a game. Per turn, the player pays a fee and throws 8 marbles onto a board, so they land in holes in the board. Each hole has a score from 1 to 6. Throwing 8 dice instead can also be done. The scores are added to form a score from 8 to 48. This score is translated into points via table/chart. The points are accumulated across turns. When the player reaches 100 points, it wins a prize. Some scores increase the number of prizes when 100 points are reached. A score of 29 doubles the fee per turn, multiplicatively, so scoring 29 10 times increases the fee to 1024x the initial fee.
The trick is that the most common scores (22-34) do not give any points. This means that only 2.7% of the turns by fair dice rolls give out points, needing 369.5 turns to reach 100 points. For the board in the video, only 0.28% give points, resulting in 5000+ turns to get 100 points. The probability to score 29 is about 8%, this leads to massive fees when playing lots of turns.
import random, numpy
import matplotlib.pyplot as plt
# return one int with random value [1,6], with the probability density described in rawMassDist
# every 1000 turns, sample 1000 loaded die throws and put them in a list
randoms = []
idxRandom = 0
def throwLoadedDie():
global idxRandom
global randoms
rawMassDist = [11, 17, 39, 44, 21, 11]
#rawMassDist = [50, 5, 5, 5, 5, 50]
massDist = [float(i)/sum(rawMassDist) for i in rawMassDist]
if (idxRandom % 1000) == 0:
#randoms = numpy.random.choice(range(1, 7), size=1000, p=massDist)
randoms = random.choices(range(1,7), massDist, k=1000)
idxRandom = 0
idxRandom += 1
return randoms[idxRandom-1]
# throw 8 dice, fairDice indicates whether fair dice or loaded dice are used
# returns the sum of the dice values, which equals the score for this turn
def throwDice():
total = 0
for _ in range(0,8):
if fairDice:
total += random.randint(1,6);
else:
total += throwLoadedDie()
return total
# translates the score into points using dictionary toPoints
def getPoints(score):
toPoints = {8:100, 9:100, 10:50, 11:30, 12:50,
13:50, 14:20, 15:15, 16:10, 17:5,
39:5, 40:5, 41:15, 42:20, 43:50,
44:50, 45:50, 46:50, 47:50, 48:100}
if score in toPoints:
return toPoints[score]
return 0
# returns if this score results in an extra price
def isExtraPrize(score):
if (18 <= score <= 21) or (score == 29) or (35 <= score <= 38):
return True
return False
# returns if this score doubles the fee for one turn
def needDoubleFee(score):
return score == 29
# simulate one turn, return the new number of points, prizes and fee for the next turn
def simulateTurn(points, prizes, fee):
score = throwDice()
if isExtraPrize(score):
prizes += 1
if needDoubleFee(score):
fee *= 2
points += getPoints(score)
return [points, prizes, fee, score]
# simulate single game, can result in win or loss in maxTurns turns
# can print result and histogram of scores
def playGame(printResult = True, maxTurns = 1000):
points = 0
prizes = 1
hist = list() # start with empty list, add score after every turn
hist2 = [0]*49 # entries 0-7 is always 0, other entries 8-48 represent the number of times a score has occurred
fee = 1
totalFee = 0
goal = 100
won = False
for turn in range(1, maxTurns+1):
#print('Turn {0}, points: {1}'.format(turn, points))
totalFee += fee
[points, prizes, fee, score] = simulateTurn(points, prizes, fee)
hist.append(score)
if points >= goal:
won = True
break
# finalize
[hist2, _] = numpy.histogram(hist, bins=49, range=[0,48])
if printResult:
if won:
print('You win {0} prizes in {1} turns, cost: {2}'.format(prizes, turn, totalFee))
else:
print('You only got {0} points in {1} turns, cost: {2}'.format(points, turn, totalFee))
print(hist2)
if not won:
prizes = 0
return [prizes, turn, totalFee, hist2]
# simulate multiple games, allow many turns per game to practically ensure win
# also disable result printing in each game
def playGames(numGames, plot=False):
hist = [0]*49
totalPrizes = 0
totalTurns = 0
totalFee = 0
withPoints = 0
gamesLost = 0
for i in range(0, numGames):
[prizes, turns, fee, hist2] = playGame(False, 100000)
if prizes == 0:
gamesLost += 1
hist = [x + y for x, y in zip(hist, hist2)]
totalPrizes += prizes
totalFee += fee
totalTurns += turns
for i in range(8, 18):
withPoints += hist[i]
for i in range(39, 49):
withPoints += hist[i]
print('{0} games, lost {1}'.format(numGames, gamesLost))
print('Avg prizes: {}'.format(totalPrizes/numGames))
print('Avg turns: {}'.format(totalTurns/numGames))
print('Avg fee: {}'.format(totalFee/numGames))
print(hist)
print('Percentage turns with points: {:.2f}'.format(100.0*withPoints/sum(hist)))
if plot:
# create list of colors to color each bar differently
colors = [item for sublist in [['red']*18, ['blue']*21, ['red']*10] for item in sublist]
plt.bar(range(0, 49), hist, color=colors)
plt.title('Score distribution across multiple games')
plt.xlabel('Score = sum of 8 dice')
plt.ylabel('Number of turns')
plt.text(40, 0.6*max(hist), 'Red bars\ngive points')
plt.show()
fairDice = False
#playGame()
playGames(100, plot=True)
Concrete questions:
- Since calling
random.choices()
has some overhead, I generate 1000 loaded die rolls and put it in a global array. Is there a better of doing this without classes? In C I'd probably use static variables. - To generate a histogram of all the scores during a game, I append to a list every turn, and then generate the histogram. Is this efficient performance-wise?
- How are my names? Especially
hist
,hist2
,isExtraPrize()
andneedDoubleFee()
. - My Ryzen 5 2400G with 3200 MHz RAM takes about 15s to simulate 100 loaded games, averaging. 3550 turns per game. I somehow feel like this should be faster, any performance related suggestions are welcome.
- And of course, general code review answers are welcome.