473,683 Members | 3,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Slightlly OT - Bingo problem

I may have mentioned that I run an Introduction to PHP course at a local
college (very basic - I'm no PHP expert). Well, one of my students was
doing really well so I set him some extension work. The task was to use
PHP to generate a random bingo card. The standard UK card has nine rows
and three columns. Each row has five numbers. All numbers are
different, out of a pool of 90.

I asked my student to design one card. He came back a few days later
with a solution that generated six cards, using each of the90 numbers
just once. Or so I thought.

Although I'd set the problem I had not coded the solution myself so I
set to it. I tried various methods but could not get a script which
would work reliably every time. More often than not I could not get all
the numbers to fit. I eventually solved the problem by brute force, ie
I discard all attempts that don't work. See
http://www.ckdog.co.uk/php/test/bingo.php for my solution
Code is here:
http://www.ckdog.co.uk/php/test/bingo_code.phps

I was still smarting because I thought my student had come up with a
better solution than me so I took another look at his. He had the same
problem, occasionally, the last line was not complete. It's a bit like
the card game patience, sometimes it doesn't work out. If you are
reading this Craig, don't look at my solution :-)

The question I have is this. Is it possible to write an algorithm that
will place all 90 numbers in the matrix in a random fashion that will
not have to loop because of failed attempts?

Please help this poor lecturer stay one step ahead of his students. :-)
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #1
33 3704
Geoff Berrow wrote:
I may have mentioned that I run an Introduction to PHP course at a local
college (very basic - I'm no PHP expert). Well, one of my students was
doing really well so I set him some extension work. The task was to use
PHP to generate a random bingo card. The standard UK card has nine rows
and three columns. Each row has five numbers. All numbers are
different, out of a pool of 90.
I guess you mean three rows and nine columns (so that 5 numbers in each
row sum up to 15 numbers per card).

I asked my student to design one card. He came back a few days later
with a solution that generated six cards, using each of the90 numbers
just once. Or so I thought.

<snip>

The question I have is this. Is it possible to write an algorithm that
will place all 90 numbers in the matrix in a random fashion that will
not have to loop because of failed attempts?
This solution seems trivial to me, so it must be wrong:

1. Generate a random permutation of the 90 numbers.
(Google knows how to do it.)

2. Split the permutation in 6 blocks of 15 numbers.

3. Sort each block.

4. Assign each block to a card.

Am I missing something?

Please help this poor lecturer stay one step ahead of his students. :-)

Jul 17 '05 #2
.oO(Geoff Berrow)
The question I have is this. Is it possible to write an algorithm that
will place all 90 numbers in the matrix in a random fashion that will
not have to loop because of failed attempts?


There's a mathematical approach for doing that (can't remember exactly,
would have to look it up), but PHP is already capable of doing it.

A quick 'n dirty hack:

<?php
// too lazy to write HTML tables ... ;-D
header('Content-type: text/plain');

// create array with 90 numbers, ordered randomly
$numbers = range(1, 90);
shuffle($number s);

// create six cards
for ($i = 0; $i < 6; $i++) {
// get 15 numbers for each card and fill up to 27 with empty elements
$card = array_pad(array _slice($numbers , $i * 15, 15), 27, 0);
// randomize positions on the card
shuffle($card);
// print the card out
for ($j = 0; $j < 27; $j++) {
print $card[$j] != 0 ? sprintf(' %02u', $card[$j]) : ' ..';
print ($j + 1) % 9 ? '' : "\n";
}
print "\n\n";
}
?>

HTH
Micha
Jul 17 '05 #3
Geoff Berrow wrote:
The question I have is this. Is it possible to write an algorithm
that will place all 90 numbers in the matrix in a random fashion that
will not have to loop because of failed attempts?


Something like this?

http://www.jwscripts.com/playground/bingo.phps
JW

Jul 17 '05 #4
.oO(Michael Fesser)
A quick 'n dirty hack:
[...]


OK, I missed that 5-per-row issue (never played Bingo), but it should
not be hard to add.

Micha
Jul 17 '05 #5
I noticed that Message-ID: <41************ ***********@new s.euronet.nl>
from Janwillem Borleffs contained the following:
The question I have is this. Is it possible to write an algorithm
that will place all 90 numbers in the matrix in a random fashion that
will not have to loop because of failed attempts?


Something like this?

http://www.jwscripts.com/playground/bingo.phps


Nice but I forgot to give you all some information.

The numbers have to be aligned so that people can mark them off easily.
That means 1-9 go in column 1, 10-19 in column 2 20-29 in column 3 and
so on. The last column has numbers in the range 80-90

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #6
I noticed that Message-ID: <cq**********@n ews.ya.com> from Dani CS
contained the following:
I guess you mean three rows and nine columns (so that 5 numbers in each
row sum up to 15 numbers per card).

Doh! Yes of course. I've been working on this too long...

I asked my student to design one card. He came back a few days later
with a solution that generated six cards, using each of the90 numbers
just once. Or so I thought.


<snip>

The question I have is this. Is it possible to write an algorithm that
will place all 90 numbers in the matrix in a random fashion that will
not have to loop because of failed attempts?


This solution seems trivial to me, so it must be wrong:


Yes, I didn't give you all the information. The columns represent the
ranges 1-9, 10-19, 20-29, and so on up to 80-90 for the last one.

It follows then that for each card there cannot be more than 3 numbers
from each range.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #7
I noticed that Message-ID: <sl************ *************** *****@4ax.com>
from Geoff Berrow contained the following:
Something like this?

http://www.jwscripts.com/playground/bingo.phps


Nice but I forgot to give you all some information.


However, it also prints the same cards each time.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #8
On Sat, 18 Dec 2004 09:57:50 +0000, Geoff Berrow <bl******@ckdog .co.uk> wrote:
Please help this poor lecturer stay one step ahead of his students. :-)


I think he might have caught on to the trick - there's a Bingo post on
alt.comp.lang.p hp ;-)

--
Andy Hassall / <an**@andyh.co. uk> / <http://www.andyh.co.uk >
<http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #9
.oO(Geoff Berrow)
The question I have is this. Is it possible to write an algorithm that
will place all 90 numbers in the matrix in a random fashion that will
not have to loop because of failed attempts?


Another idea ...

It should be possible to set all numbers on all six cards in a way
similar to the eight-queens-problem.

There are some defined constraints on the rows and columns, which can be
easily checked:

* There are 18 rows, splitted into 6*3. Each row holds 5 numbers.
* There are 9 columnss, the first contains the numbers 1-9, the next
10-19 and so on, the last one contains 80-90. So you always know which
column a number belongs to, you just have to find a row that isn't
completed yet (has less than 5 numbers).

So consider all six 9*3 cards as one single 9*18 card, then it should be
as follows:

* Get a number.
* Determine its column.
* Put it in the first row that has less than 5 numbers.
* Move on to the next number.

If at some point a number can't be set because there's no free row
available for the required column, step back to the last set number and
change their position, then continue from there (and if the position
can't be changed anymore move back another step). This backtracking is
quite easy to do with recursive algorithms.

Just the basic idea ...

Micha
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4011
by: boonie | last post by:
hi, i have to play bingo in vb , but im really stuck....i just need very simple code to create random numbers in one label.....thanks for your help
8
2521
by: GA | last post by:
I have a weird CSS problem at: In firefox, the most recent version, none of the links work: header photo/logo is supposed to link, the buttons on the right are rollover links. I can't even select the text with my mouse in firefox! Everything is fine in IE..... http://copland.udel.edu/stu-org/foodscience/index.html
5
5923
by: Fred Hebert | last post by:
I was thinking of switching to VS2005, so I sent off for and received a 120 evaluation kit. This version was supposed to be the same as the full version, but the key limits you to 120 days. I installed it and began learning how to use it. I built several small C# test apps that use common classes in a dll that I also wrote. When I right click on a class that was defined in the dll and select "go to definition" it would open the source...
2
2635
by: Explore_Imagination | last post by:
The task is to solve a constrained optimization problem in C. Computational Time is of high priority. One approach can be to use ready functions in a "free ware" Optimization Library (if available). If any one of you have any idea about such library please inform me. I am dealing with Constrained Optimization for the first time so please guide me How I should solve this problem. I will appreciate suggestions from you. Find values of x...
9
5008
by: Fsunka | last post by:
Hey, I have a project due around February 28. I have to create a program that asks the user for the number of players that want to play bingo. Then it creates that many bingo cards. Then it calls out BINGO numbers, but it can't make a repeat call out. Also, when something is called out, I have to somehow mark on the card that it has been called out. When the person gets 5 horizontally or vertically (not diagonally) then the programs says...
12
11249
by: hollis092 | last post by:
I am writing code for the game of BINGO. The code below shows a function that fills the card with random numbers. A random number in each location, 0-15 for B column; 16-30 for I column, 31-45 for N column, 46-60 for G column, and 61-75 in O column. This is a 5X5 2 D array. I am having problems with this function, it isnt working as it should. No compile error, but output is nothing but a flashing cursor, maybe this is an infinite loop??...
1
238
by: bingo | last post by:
Hi, All: I'm really new to C++, so please forgive If I asked stupid questions: I was trying to use the following Vector class to do some calculations: ============================= class Vector { public:
7
1945
by: Petr Vileta \(fidokomik\) | last post by:
I have a form with few text inputs. Every text input is followed by image type input. In this form I want to have 1 submit button on the top. A problem I want to resolve is: when user type something into text input and press enter then I want to submit form by image type button but not by submit button. I tried to use tabindex parameter but this not work as I expect. Any idea? Code example:...
1
3799
omerbutt
by: omerbutt | last post by:
hi every one i have a menu li and ul based the problem is when any specific category in the li is hovered the li or the sub-cat items appear but as i move my mouse over the sub-cat or level two li it disappears can any one help in this here is the link you can watch the left menu here link here is the code var menuids= //Enter id(s) of SuckerTree UL menus, separated by commas function buildsubmenus(){ for (var i=0; i<menuids.length;...
0
8568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8504
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8753
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6429
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5789
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4519
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2934
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2195
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.