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);
}
?>
@oelna
Copy link
Author

oelna commented Apr 3, 2017

If this helps you, I would love to hear from you. How did you use it, what did you change?

Btw: I just let the script generate 10000 cards and it took about 5-10 seconds, so not the biggest deal.

@ydelwiche
Copy link

Works great, but if you want real random values (currently they are sorted column by column), you should use shuffle() instead:

$random_values = $columns[$j];
shuffle($random_values);
$bingo_card = array_merge($bingo_card, array_slice($random_values, 0, 5));

@prispens
Copy link

Seems like the script doesn't add the $card_hash to $card_hashes, so probably add $card_hashes[] = $card_hash; after line 50.

@oelna
Copy link
Author

oelna commented Jun 22, 2019

Seems like the script doesn't add the $card_hash to $card_hashes, so probably add $card_hashes[] = $card_hash; after line 50.

@prispens You are of course correct! I'm such a doofus, making the hashing solution in the first place and never implementing it correctly. Thanks!

@oelna
Copy link
Author

oelna commented Jun 22, 2019

@ydelwiche If I remember correctly (I'm sort of out of the game now, but I researched the rules somewhat back in 2017 when I made this), the "sorted by value" way is the "official" way Bingo cards should be done (to facilitate the checking of numbers), even though I can't find proof right now. Thanks for your contribution though! It feels well thought out.

@TheCire
Copy link

TheCire commented Oct 9, 2019

Quick question, I'd like to be able to generate the same set of cards given a seed, is that possible to update this algorithm to allow for a seed?

@oelna
Copy link
Author

oelna commented Oct 28, 2019

I'm sorry, I have not built in any support for seeds. You are on your own on this. Good luck! If anything comes of it, you're welcome to share the results, though!

@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