Skip to content

Instantly share code, notes, and snippets.

@oelna
Last active Mar 5, 2021
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@oelna 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

This comment has been minimized.

Copy link

@ydelwiche ydelwiche commented Feb 7, 2019

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

This comment has been minimized.

Copy link

@prispens prispens commented Jun 20, 2019

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

@oelna

This comment has been minimized.

Copy link
Owner Author

@oelna 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

This comment has been minimized.

Copy link
Owner Author

@oelna 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

This comment has been minimized.

Copy link

@TheCire 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

This comment has been minimized.

Copy link
Owner Author

@oelna 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

This comment has been minimized.

Copy link

@xuvious 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