views:

43

answers:

1

Hello Everyone, I'm trying to create a typical Housie/Bingo Game ticket in Asp.net 2.0 (VB). But, not being succeeded. Ticket contains 3 row with 9 columns. Total 27 blocks, and it must be only 15 should be fill outta those 27. and each column contains value like 1st column should be between 1-10 and 2nd must have random values between 11-20.. It doesn't matter how many blocks filled in each column.. 1 is must, no single column should be blank, all 9 columns must be filled, some have all 3 blocks filled, some have 1 with total of 15 blocks filled in whole ticket. with random numbers..

Guys here was the typical housie ticket specification.. Please help me out generating a ticket like this. I've tried but, not being that much succeeded, I get whole column blank and validating columns.. takes extra load on system. please find me the way to make it.

+1  A: 

You need deterministic algorithm

First redefine your requirements:

  1. Every column should have at least 1 number, so no column should be completely blank
  2. First column should have numbers from 1..10, second 11..20, third 21..30 and so on to the ninth column having from 81..90
  3. only 15 numbers needed to fill the ticket
  4. Additional requirement: Every row should have 5 numbers

This is how I'd do it:

  1. First select 9 randoms (to satisfy first requirement)
    • 1..10 - one random number from this range
    • 11..20 - one random number from this range
    • ...
    • 81..90 - one random number from this range
  2. Prepare an array (selectNums) of numbers 1..90 and remove all selected in step 1
  3. Loop
    1. get a random number from the selectNums array
    2. add it to your ticket and remove it from the selectNums aray
    3. if selected number fills a column of three then remove all numbers from that range from the selectNums array.
    4. Go back to step 1 in the loop

This algorithm will take you exactly 9 steps + 6 steps in the loop so it's deterministic which is better for the processor utilization. And it also fills your ticket with up to three numbers per column and not less than 1 (if I understood your requirements due too poor english in the question).

Yo when you select random numbers you always select a number between 0 and selectNums array length which will give you position in array where you have to take a number from.

Additonal functionality to create the actual ticket

Above steps will get you to a point where you will get exactly 15 numbers with at most 3 from the same ten numbers range. Fine. All you have to do now is to create your ticket.

  1. define 3 variables: row1Count, row2Count and row3Count and set them all to 0.
  2. Fill the ticket by starting from the fully filled columns (all three numbers):
    1. Get the first full column and fill it up in the ticket while also incrementing all three variables by one.
    2. Remove these numbers from the selectNums array.
    3. Go back to step 2.1.
  3. Fill the ticket with columns with two numbers:
    1. Get the first two numbers column. Fill them in the ticket by using three possible permutations of filling them in (1&2, 2&3, 1&3). Fill the first pair using the first permutation, second one with the second and so on. Don't forget to increment corresponding row counter variables.
    2. remove those two numbers from selectNums array
    3. Go back to step 3.1.
  4. Fill the ticket with single number columns (those that have just one number):
    1. Get the first number from selectNums array and put it in the row with the smallest count and put in on the ticket in that particular row. When there are at least two rows with the same count, you can select whichever you prefer by either selecting one randomly or taking the first one (quickest).
    2. Remove the number from selectNums array
    3. Go back to 4.1.

This part should get you to fully filled ticket with all columns having at least one number and all rows containing exactly 5 numbers in total.

If smaller numbers are not allowed to be under larger ones, you can always add an additional step to this process and reorder numbers in those columns that have more than just one number in it.

One final observation

This solution has been simplified by using arrays and counters. You could of course create a complete object model that would be functionally rich and would provide you all the info you need. You could for instance have a Ticket and TicketColumn classes:

public class TicketColumn
{
    public int Count { get; }
    public int? FirstRowValue { get; set; }
    public int? SecondRowValue { get; set; }
    public int? ThirdRowValue { get; set; }

    ...
    public void Reorder() { ... }
}

public class Ticket
{
    public TicketColumn[] Columns

    public int FirstRowCount { get; private set; }
    public int SecondRowCount { get; private set; }
    public int ThirdRowCount { get; private set; }

    ...
}

Or something similar. This is just an idea that this whole program would be better off in object oriented manner.

Robert Koritnik
Thanks for your reply Robert, You got it right pal. But, I want to generate thousands of tickets. I understood 1st step which you just mentioned to take 9 random values from 1-10, 11-20 ... and I can get 6 more values randomly.. but, Its about feeling in blocks each column contains 3 block so, its also needed that blocks fill randomly.. in some columns 1 block filled, in some columns all three filled, but total of 15 blocks needes to be filled without any blank column. each row contains 5 blocks filled. Please help me out buddy. thanks in advance.
idleMind
@idleMind: So you've left out one requirement or what? So each column needs to have at least one number (this is done by the first step of my algorithm) but... As I understand your requirement in your comment: Each row must also have **at least** 5 numbers as well? How do you know which numbers go into which row? Could you please amend your question and write down **all** requirements.
Robert Koritnik
@ideleMind: The loop part (STEP 3) does take care that you select all other numbers numbers randomly. So they can fall into any column.
Robert Koritnik
thats what I'm telling you @Robert, you are right, we'll get 9 random number for each columns.. thats true.. but, those 3 blocks of every columns.. should also be selected randomly.. u knw
idleMind
I mean I can get 15 random values for whole ticket, but main question is how to fill them in random blocks, with specifications like each row must contain 5 filld blocks.. and no column should be blank, at least 1 block outta three must be filled. get it?
idleMind
@idleMind: I'm still not 100% sure I understand. Columns (hence numbers as well) after initial step are selected randomly (6 times). Is there a similar row requirement as it is for columns? Like 1.column must have numbers from 1..10... Is there anything similar for rows as well? Like first row 1,2,3,11,12,13,21,22,23... etc.?
Robert Koritnik
@Robert: Check out the image of Housie Ticket: http://www.topbettings.com/images/Housieticket.jpg . and description of the ticket is here: http://www.topbettings.com/Housie.html
idleMind
and yes, columns must have values in its range.. like first column can only have values between 1 to 9.. and 2nd column have values between 10-19, 3rd column values 20-29. and so on.. and last 9th column's values 80-90.. as you can see in above image of housie ticket. no column is blank in there.. all three rows have 5 blocks filled. and total 15 filled blocks. I want to generate that kinda ticket randomly.
idleMind
@idleMind: I didn't ask for column number requirements (we've specified that already). I asked for **row** number requirements.
Robert Koritnik
no no.. there's nothing particular for rows robert.. all three rows contains 9 blocks..?? right? there's nothing specific for rows.. its only for columns.. and block comes under any column.. should be between specified values?? you can see that in that Housie Ticket Image I sent you
idleMind
@idelMind: I have now edited my answer with the additional algorithm that will produce a reliably filled ticket. Maybe there is a flaw in the process but I'm sure you'll be able to slightly change it to work as expected.
Robert Koritnik
@Rober.. I'll try what you just mentioned above. :) Will let you know very soon. Thanks a lot.
idleMind