Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Project: Peter's Files
Views: 3893
Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu1804
Kernel: Python 3 (system-wide)

Bingo Simulator

import numpy as np import random as r
# returns a random bingo board def newBoard(): B = np.array(r.sample(range(1,16),5)) I = np.array(r.sample(range(16,31),5)) N = np.array(r.sample(range(31,46),5)) N[2] = 0 G = np.array(r.sample(range(46,61),5)) O = np.array(r.sample(range(61,76),5)) return np.vstack((B,I,N,G,O)).transpose()
# Checks if a bingo board `board` is a winner given called numbers `called` # returns an numpy truth array of winner of type [R C D] def isAWinner(board:np.array, called:list): blank = np.ones((5,5)) blank[2][2]=0 # This will make `blank` have a zero in boxes that `board` has numnbers that were in `called` for i in called: blank *= (board - i) # Check for row of zeros R = any([all(blank[i]==0) for i in range(5)]) # Check for column of zeros blank = blank.transpose() C = any([all(blank[i]==0) for i in range(5)]) # check for diagonals of zeros D = all([blank[i][i]==0 for i in range(5)]) or all([blank[i][4-i]==0 for i in range(5)]) return np.array([R,C,D])
# we will collect our data in `winners` winners = np.array([0,0,0]) # play 100,000 games for game in range(100_000): # in each game call random numbers from 1 to 75 call = r.sample(range(1,76),75) # there are 1000 people playing each game boards = [newBoard() for i in range(1_000)] # increase the number of called balls for and check for a winner in the 1000 boards breakAfterThisBall = False for ball in range(1,76): called = call[:ball] for board in boards: test = isAWinner(board, called) # if there is a winner, record the types of winners if sum(test) >= 1: winners += test breakAfterThisBall = True # Break after the round that found a winner if breakAfterThisBall: break
# 100_000 games with 1000 boards each [49125 46586 63840]
# at least 421755 but probably around 700_000 games with 100 boards each [396054 373104 421755]