Skip to content

Instantly share code, notes, and snippets.

@oelna
Last active March 5, 2021 00:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oelna/60e6a1e59fe89fccff1e93791e3a086b to your computer and use it in GitHub Desktop.
Save oelna/60e6a1e59fe89fccff1e93791e3a086b to your computer and use it in GitHub Desktop.
A generator for unique Bingo cards in PHP. See in action https://bingo.arnorichter.de/ (generates cards printable on A4 paper size)
<?php
/*
Generate however many unique bingo cards you require (should be fewer than 10k though).
This is not meant to compute ALL possible bingo cards, obviously, but to help set up a
nice amount of cards for play.
If you require a free space somewhere on the board, you should probably tweak the output,
not the generator.
I wrote this because existing solutions on stackoverflow were in languages I could not
test as easily and many existing generator websites generate crap cards that do not
adhere to the bingo number distribution in the 5 rows, so you spend an eternity looking
up your numbers.
I hope this helps somebody. Let me know if you find it useful or you know how to improve
the code. If you can spare a dollar or two, I appreciate gifts! https://paypal.me/oelna
Created: April 4, 2017
Updated: June 6, 2019
By: Arno Richter
URL: http://bingo.arnorichter.de/
*/
DEFINE('BR', "<br />\n");
$number_of_cards = 60; // the amount of unique cards to generate. don't make too many!
$columns = array(
range(1,15),
range(16,30),
range(31,45),
range(46,60),
range(61,75)
);
$bingo_cards = array();
$card_hashes = array();
$i = 0;
/* GENERATE THE CARDS */
while($i < $number_of_cards) {
$bingo_card = array();
for($j=0; $j<5; $j++) {
$random_keys = array_rand($columns[$j], 5);
$random_values = array_intersect_key($columns[$j], array_flip($random_keys)); // http://stackoverflow.com/a/18047331/3625228
$bingo_card = array_merge($bingo_card, $random_values);
}
// generate a unique hash for this card and compare it to the ones we already have
$card_hash = md5(json_encode($bingo_card)); // or whatever hashing algorithm is preferred
if(!in_array($card_hash, $card_hashes)) {
$bingo_cards[] = $bingo_card;
$card_hashes[] = $card_hash;
$i += 1;
}
if($i > 10000) break; // safety exit
}
/* OUTPUT, if needed (output with monospace font). could be made into an html table. */
foreach($bingo_cards as $card) {
for($k=0; $k<(sizeof($card)/5); $k++) {
echo(str_pad($card[$k], 2, ' ', STR_PAD_LEFT).' | ');
echo($card[$k+5].' | ');
echo($card[$k+10].' | ');
echo($card[$k+15].' | ');
echo($card[$k+20].BR);
if($k < 4) echo(str_repeat('-', 22).BR);
}
echo(BR.BR);
}
?>
@xuvious
Copy link

xuvious commented Apr 19, 2020

Hi there the code seems to work perfectly however in your example the numbers are formatted in tables with instructions and are sized A4, adding the code above to my page generates beautifully but the formatting is missing - do you have a code example with the formatting, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment