Python Bingo Card Generator

April 9, 2008

Here is a Python script I wrote some time ago. It generates bingo cards based on a number of variables at the top of the file, such as grid size, and number of cards.

#!/usr/bin/env python
import random

gridSize = 5
minNum = 1
maxNum = 50
cards = 40

for h in range(cards):
	card = []
	randRange = range(minNum, maxNum)
	card = random.sample(randRange, gridSize * gridSize)
	for i in range(gridSize):
		string = ""
		for j in range(gridSize):
			string +=  str(card[i + j * gridSize]) + "t"
		print string	

	print "n"

Below if the output when cards is set to 2:

~$ ./bingo.py
11      39      48      19      36
3       12      29      32      31
8       16      5       34      22
14      47      21      26      23
4       13      42      44      28

28      8       15      16      42
10      35      43      38      41
27      17      21      1       4
9       47      11      30      40
49      44      6       45      18

1 Comment »

  1. Thanks for the inspiration, here’s my own little hack with a few tweaks. It outputs an HTML file with page breaks built in so they can be printed. In addition, rather than choosing 25 random numbers, each column is from a range (corresponding to the stepping) and is ordered. The HTML itself isn’t particularly well done but as I said it’s a quick hack, hope the formatting shows up okay:

    [code]
    import random

    numbersheets = 10
    gridsize = 5
    stepping = 15

    f = open("/pathtosaveto/bingosheet.html", "w")

    f.write("\n\n")

    for i in range(numbersheets):
    f.write('\n')
    f.write("\nB\nI\nN\nG\nO\n\n")

    bingosheet = []
    for j in range(gridsize):
    choices = range(((j * stepping) + 1), ((j * stepping) + 16))
    numbers = random.sample(choices, gridsize)
    numbers.sort()
    bingosheet.append(numbers)

    bingosheet = [[r[col] for r in bingosheet] for col in range(len(bingosheet[0]))]
    bingosheet[2][2] = "*"

    for row in bingosheet:
    f.write("")
    for column in row:
    columnstring = '' + str(column) + ""
    f.write(columnstring)
    f.write("\n")
    f.write("\n")

    f.write("\n")

    f.close()
    [/code]

    Comment by Vernon — February 27, 2009 @ 8:23 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment