chatgpt writing a function in harbour
Posted: Sat Feb 18, 2023 8:05 pm
Hi all,
My grandson (he is studying IT on Technical University Munic, had a discussion about harbour and artificial intelligence. He was sure chatgpt would know harbour. I had a doubt that AI would be able to write program-code in harbour. So he required a function that would generate 6 numbers withina range fro 1 to 49 (lotto), and each number should appear only once.
It lasted 3 seconds and the code was available.
After compiling I got the error unknown function swap() and that random function was defined multiple.
Here the codeof GenerateLottoNumbers
I had written a function myself that works fine, but I think the Function swap() by AI needs a self written function to do the same.
My function uses a complete different method to do the job. Maybe its of interest, so here my function:
I really was impressed but the drawback? Will in the future humans write programs any longer.
Hope I didn't bother you.
Regards
Robert
My grandson (he is studying IT on Technical University Munic, had a discussion about harbour and artificial intelligence. He was sure chatgpt would know harbour. I had a doubt that AI would be able to write program-code in harbour. So he required a function that would generate 6 numbers withina range fro 1 to 49 (lotto), and each number should appear only once.
It lasted 3 seconds and the code was available.
After compiling I got the error unknown function swap() and that random function was defined multiple.
Here the codeof GenerateLottoNumbers
Code: Select all
#include "hmg.ch"
FUNCTION Main()
public zahl,nI,rndnum
public lotto[6] &&array für 6 Zahlen
aSTRUCT_L := {} &&array für db_structure lotto.dbf
AADD( aSTRUCT_L , { 'spiel ' , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl1 ' , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl2 ' , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl3 ' , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl4 ' , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl5 ' , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl6 ' , 'N' , 2, 0 } )
DBCREATE( "lotto.dbf" , aSTRUCT_L, "dbfntx" ) &&index file ntx
aSTRUCT_V := {}
AADD( aSTRUCT_V , { 'zahl ' , 'N' , 2, 0 } )
AADD( aSTRUCT_V , { 'J_N ' , 'L' , 1, 0 } ) &&nur Wahrheitswet .T. oder .F. möglich
DBCREATE( "valnum.dbf" , aStruct_V, "dbfntx" )
use valnum via "dbfntx"
for nI=1 to 49 &&Zalen 1-49
dbappend()
replace zahl with nI,J_N with .T. && Wahrheitswert .T. für alle Datensätze
next
index on zahl to zahl &&Indexdatei zahl.ntx
**********************************************************
DEFINE WINDOW crtnum_1 ;
WINDOWTYPE MAIN ;
ROW 10 ;
COL 10 ;
WIDTH 800 ;
HEIGHT 700 ;
FONTNAME 'ARIAL' FONTSIZE 14 ;
On init GenerateLottoNumbers()
end window
*************************************************************
crtnum_1.activate
crtnum_1.center
FUNCTION generateLottoNumbers
LOCAL aNumbers := {}
LOCAL nCount
// Füllt das Array mit Zahlen von 1 bis 49
FOR nCount := 1 TO 49
aNumbers[nCount] := nCount
NEXT
// Mischen Sie das Array zufällig
FOR nCount := 1 TO 49
nRandom := INT(RAND() * 49) + 1
SWAP(aNumbers[nCount], aNumbers[nRandom])
NEXT
// Speichern Sie die ersten sechs Zahlen in der Datenbank
USE lotto.dbf
APPEND BLANK
REPLACE number1 WITH aNumbers[1]
REPLACE number2 WITH aNumbers[2]
REPLACE number3 WITH aNumbers[3]
REPLACE number4 WITH aNumbers[4]
REPLACE number5 WITH aNumbers[5]
REPLACE number6 WITH aNumbers[6]
PACK
CLOSE DATABASES
return
My function uses a complete different method to do the job. Maybe its of interest, so here my function:
Code: Select all
#include "hmg.ch"
FUNCTION Main()
public zahl,nI,rndnum
public lotto[6]
aSTRUCT_L := {}
AADD( aSTRUCT_L , { 'spiel ' , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl1 ' , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl2 ' , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl3 ' , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl4 ' , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl5 ' , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl6 ' , 'N' , 2, 0 } )
DBCREATE( "lotto.dbf" , aSTRUCT_L, "dbfntx" ) &&index file ntx
aSTRUCT_V := {}
AADD( aSTRUCT_V , { 'zahl ' , 'N' , 2, 0 } )
AADD( aSTRUCT_V , { 'J_N ' , 'L' , 1, 0 } )
DBCREATE( "valnum.dbf" , aStruct_V, "dbfntx" )
use valnum via "dbfntx"
for nI=1 to 49 &&Zalen 1-49
dbappend()
replace zahl with nI,J_N with .T. &&all records get flag .T.
next
index on zahl to zahl &&Indexfilei zahl.ntx
**********************************************************
DEFINE WINDOW crtnum_1 ;
WINDOWTYPE MAIN ;
ROW 10 ;
COL 10 ;
WIDTH 800 ;
HEIGHT 700 ;
FONTNAME 'ARIAL' FONTSIZE 14 ;
On init crtnum()
end window
*************************************************************
crtnum_1.activate
crtnum_1.center
function crtnum
select valnum &&order
for nI=1 to 6
rndnum=HB_RandomInt(1,49) &&Zahlen von 1-49 möglich
seek rndnum
IF valnum->J_N=.T.
lotto[nI]=rndnum
replace J_N with .F. && now the number created is no longer available
ELSE
nI--
Endif
next
replace all J_N with .T. && for the next pass are true again
asort(lotto) &&sorting array lotto ascending.
** the 6 fields of the table lotto are Z AHL1,ZAHL2,ZAHL3,ZAHL4,ZAHL5,ZAHL6
use lotto
dbappend() &&Datensatz anfügen
for ni=1 to 6
cfeld='ZAHL'+ltrim(str(ni))
replace &cfeld with lotto[ni] && Makro-Operator selects fieldnames
next
crtnum_1.release &&Programmende
return
.
Hope I didn't bother you.
Regards
Robert