Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number generator comapred to number list

Status
Not open for further replies.

Micwiz

Technical User
Nov 9, 2013
3
GB
Hello there

I am writing a program to generate 39 combinations of numbers into rows of 5 each. I managed to do this with the FOR ...NEXT function. I also have a list of numbers I want to compare it to.These are 600 lines of numbers with 5 numbers on each line.If a generated number matches 5 numbers in a line in the database,it will ignore this and GOTO the next generated number until an unmatched number does not come up,in this case it prints to the screen and is counted The problem is this database of numbers is too big to be added to the program code with the DATA and READ statements,so I have to store the date base in a different file and open it to read from it. I have a problem with the program reading the file using the IF ,Then functions and the AND OR functions to compare the data it does not seem to be working.

Here's how the code looks like:

[highlight #3465A4]
FOR a = 1 TO 35
FOR b = 2 TO 36
FOR c = 3 TO 37
FOR d = 4 TO 38
FOR e = 5 to 39

IF a = b OR a > b THEN 493
IF b = c OR b > c THEN 492
IF c = d OR c > d THEN 491
IF d = e OR 8d > e THEN 490

OPEN "list.txt" FOR INPUT AS #1
DO UNTIL EOF(1)
INPUT #1, aa,bb,cc,dd,ee

IF (aa = a) AND (bb = b) AND (cc = c) AND (dd = d) AND (ee = e) THEN GOTO 299

GOTO 300

299 EXIT DO
CLOSE #1
GOTO 489

300 LOOP
CLOSE #1

301
tot = tot + 1
print a;b;c;d;e
489 NEXT t
490 NEXT e
491 NEXT d
492 NEXT c
493 NEXT b
494 NEXT a
[/highlight]


What am I doing wrong?
Is there a better way to write the program?
Is there a way to get past the out of memory issue with Qbasic?

Ta
Mick
 
Never have done much with "Q" but with "GW" I would have set the database up as an array 35 deep by 5 wide. Comparison would be on 1st value and expand only if matched.

Seem to recall that there was an environment statement in DOS that would expand available memory.

Ed Fair
Give the wrong symptoms, get the wrong solutions.
 
Is there a better way to write the program?
Without a doubt :)

Is there a way to get past the out of memory issue with Qbasic?
Don't use 5 nested loops that read a 600 line file every iteration. (34 * 34 * 34 * 34 * 34 = WAY out of memory [for qbasic])

I don't understand what you are trying to do. Can you re-explain.

-Geates

 
Thanks EDfair. You are right. I was confused as the program has not reached its size. I changed the memory allocation in the properties section for memory.I managed to use the DATA READ function as it seems easier to do.


Basically I am generating all lottery numbers from 1 to 39 in order. It is a 5 ball draw. Then I want to compared it to a draw history of numbers that have that have already appeared.I want it to print out all these numbers and to ignore number combinations that have already appeared, e.g. if 2,5,17,23,24 has appeared, it wont print it . In my case I want to compare it to 4 numbers in a line of 5, e.g. if I have 1 2 3 4 5, there 5 number combinations to compare it to.


I managed to resolve the memory issue. I went back to using data read and now it is working. I went to setting and increased the allocated memory in properties. I was confuzzled as the file is smaller than 64kB. Geates it works but it does not give an out of memory error as it just prints to screen.

I am thinking there might be a function or another way to generate these numbers, as it is a bit slow scanning these numbers. I might add more filters later to it.
 
Several things I would test:

In creating the table of used numbers could you store them as alphanumeric rather than as numbers. Storing an array of 2 characters would be less memory intensive. Would, however, require a little more creative work in the generation of new numbers and the conversion to compare.

Rather than creating 39 new number sets at the beginning I would create them set by set and do the comparisons on the fly, stopping when I reached the 39 new ones.

How do you create the new numbers? Would assume you check for duplicates within a set.

Ed Fair
Give the wrong symptoms, get the wrong solutions.
 
Hi Edfair

As a noob I would if I could. I am comparing those numbers on the fly while the numbers are being generated. I am generating all combinations of numbers picked from 1 to 39. You will have 39!(39-4)!/5! possible combinations. you can check without opening the document for input.Duplicates are checked after the next functions.

I can do that Geates but i want ALL number compared, then compared how much tickets it will take.
 
While using strings would save on memory, using a on/off array will save on speed.

assuming your 600 line file looks like
Code:
2,6,3,23,12
4,9,20,11,12
14,8,17,3,20
11,12,14,19,20

this will find and report all the unused first digits.
Code:
DIM arrUsed(35) 'DIMing automatically sets the value to 0
OPEN "600_line_file" FOR INPUT AS #2.
   DO WHILE NOT EOF(2)
      INPUT #2,a,b,c,d,e
      arrUsed(a) = 1 'the index represents a digit.  set the value to 1 to indicate that it has been used in the past
   LOOP
CLOSE #2

PRINT "Unused first digits:"
FOR i = 0 to ubound(arrUsed)
   IF (arrUsed(i) = 0) THEN PRINT i, 'any index with a value of 0 indicates the number was not previously used.
NEXT i

It's a start..

-Geates

 
This problem intrigue me, so I "completed" it. The program reads from a file of previously used lotto combinations (5 numbers ranging from 0 to 99) and outputs 39 new unique combinations to a file call "new.txt".
For this example, the previously use combinations ("used.txt") is created by the program. Notice, how I used characters and strings to represent unused numbers - thanks, Edfair, for the very good idea.

Code:
DEFINT A-Z
CLS

CONST OFFSET = 32 'In ascii, characters start at 32.  This is so we don't use carriage returns and tabs to represent numbers

DIM arrNew$(38)
DIM arrPool$(4)
DIM arrUsed(99, 4) 'DIMing automatically sets the value to 0

'build the file of "used" lotto number sequences
OPEN "used.txt" FOR OUTPUT AS #1
   FOR l = 1 TO 5
      row$ = ""
      FOR x = 1 TO 5
         num = INT(RND * 99) + 1
         row$ = row$ + STR$(num) + ","
      NEXT x
      row$ = LEFT$(row$, LEN(row$) - 1)
      PRINT #1, row$
   NEXT l
CLOSE #1

'read the lotto file and store the results in array
OPEN "used.txt" FOR INPUT AS #2
   DO WHILE NOT EOF(2)
      INPUT #2, a, b, c, d, e
      arrUsed(a, 0) = 1
      arrUsed(b, 1) = 1
      arrUsed(c, 2) = 1
      arrUsed(d, 3) = 1
      arrUsed(e, 4) = 1
   LOOP
CLOSE #2

'compose unique lotto numbers 
intCount = 0
DO UNTIL intCount > UBOUND(arrNew$)
    strNew$ = ""
    FOR i = 0 TO UBOUND(arrPool$)
        intDigit = INT(RND * LEN(arrPool$(i))) + 1
        strChar$ = MID$(arrPool$(i), intDigit, 1)
        strNew$ = strNew$ + STR$(ASC(strChar$) - OFFSET) + ","
    NEXT i
    strNew$ = LEFT$(strNew$, LEN(strNew$) - 1)
  
    intWrite = 1
    FOR i = 0 TO UBOUND(arrNew$)
        IF (LEN(arrNew$(i)) <> 0) THEN
            IF (strNew$ = arrNew$(i)) THEN intWrite = 0
        ELSE
            EXIT FOR
        END IF
    NEXT i

    IF (intWrite = 1) THEN
        arrNew$(intCount) = strNew$
        intCount = intCount + 1
    END IF
LOOP

'output to file
OPEN "new.txt" FOR OUTPUT AS #3
    FOR i = 0 TO UBOUND(arrNew$)
        PRINT #3, arrNew$(i)
    NEXT i
CLOSE #3

-Geates

 
Oh goodness! I left out an extremely critical code block...

Code:
FOR j = 0 TO UBOUND(arrUsed, 2)
   strUnused$ = ""
   FOR x = 0 TO UBOUND(arrUsed)
      IF (arrUsed(x, j) = 0) THEN
         strUnused$ = strUnused$ + CHR$(x + OFFSET)
      END IF
   NEXT x
   arrPool$(j) = strUnused$
NEXT j

goes before "'compose unique lotto numbers"

-Geates

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top