Skip to content

Instantly share code, notes, and snippets.

@oliyoung
Created August 7, 2018 23:51
Show Gist options
  • Save oliyoung/b33c313d2453298f95190284572e010d to your computer and use it in GitHub Desktop.
Save oliyoung/b33c313d2453298f95190284572e010d to your computer and use it in GitHub Desktop.
Generates 90-ball UK bingo / 'housie' cards. The cards contain three rows and nine columns. Each row contains five numbers and four blank spaces randomly distributed along the row. Numbers are apportioned by column (1–9, 10–19, 20–29, 30–39, 40–49, 50–59, 60–69, 70–79 and 80–90).
require 'matrix'
class Card
def self.generate
collection = []
for group in 0..8
set = []
while set.length < 3
x = rand(10) + (group * 10) + 1
set.push(x) unless set.include?(x)
end
collection += set
end
collection.sort!
grouped_collections = ::Matrix.rows(collection.in_groups_of(3)).t.to_a
nulled_collection = []
grouped_collections.each do |grouped_collection|
null_indexes = (0...9).sort_by{rand}[0,4].sort
nulled_group = []
grouped_collection.each_with_index do |n, index|
if null_indexes.include?(index)
nulled_group.push(nil)
else
nulled_group.push(n)
end
end
nulled_collection.push(nulled_group)
end
::Matrix.rows(nulled_collection).t.to_a.flatten
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment