views:

313

answers:

3

Hi,

Does anyone know of an algorithm that can generate unique bingo card faces? I'm looking to implement this algorithm in C#.

Thanks,

+1  A: 

get 5 sets containing 15 numbers each (1-15 for set 1, 16-30 for set 2...)
select 5 different numbers in sets 1,2,4,5
select 4 different numbers in set 3

To check if that card already exists
Check each existing card for top left correspondance with new card
if both numbers are equal, then move to the second number
if you get 24 times the same number at the same place then both cards are equal and new card must be rejected

Eric
Maybe you could come up with a function that hashes the bingo card uniquely instead of having to compare every value?
Esteban Araya
ended up doing something slightly different but along similar lines as to what you suggested above and used a dictionary to hash the patterns of the 5 columns (reducing each column into a permutation of 5 numbers out of 15) and it seems to work quite well both in performance and memory usage
theburningmonk
I'm not sure it is more efficient since you always have to hash 24 numbers while the in if solution, you have 1 comparison in the best case scenario. It would be correct to do a little distribution probability and unless you generate a huge amount of cards, I doubt you will have that many collisions
Eric
A: 

This is an interesting problem, but as Michael Madsen reported, given the number of possibilities, you would probably be better generate them randomly and after, check if you have duplicates. (Unless you want to generate all 111 quadrillion possibilities, which I hope you have data storage space for!)

M. Joanis
A: 

Here's a function for generating a random subset of integers from a given range which you might find useful:

private static IEnumerable<int> RandomSubsetOfRange(int min, int max, int count)
{
    Random random = new Random();

    int size = max - min + 1;
    for (int i = 0; i <= size; i += 1)
    {
        if (random.NextDouble() <= ((float)count / (float)(size - i + 1)))
        {
            yield return min + i;
            count -= 1;
        }
    }
}
ICR