Page 1 of 1

random loop (avoid repetition)

Posted: Tue Jun 24, 2014 8:47 pm
by francof
hello to all,
let's see if I can explain my request.

in a db I have a table called TBLDOMANDE_VARIE which contains a series of questions divided into three different levels (1, 2 and 3)
a query extracts all the questions related to the chosen level (level 1, 2 or 3). here it is

Code: Select all

 put "SELECT DOMANDA, NUM from TBLDOMANDE_VARIE WHERE LIVELLO  = '"& tNumLiv &"' " into tSQL
    put revDataFromQuery(tab,return,tDatabaseID,tSQL) into tRecords
where DOMANDA is the text of the question
NUM is the number which identifies the question
LIVELLO is the number of the selected level

after that, I select randomly 1 of the questions contained in the recordset created by the query, so:

Code: Select all

 put any line of tRecords into  tDom
   set ItemDel to TAB
   put item 1 of tDom  into field "txtDomanda"
far so good.

Now I would like to avoid the possibility of reproposal the same question. I was thinking something like this:

- store in some way the numbers of the questions.
- verify that the number of randomly picked question does not already exist in this list (array?)
- in case extract another question.
- if all numbers are in the list (which means that all the questions have been asked), end the loop.

any help is welcome
ciao
franco

Re: random loop (avoid repetition)

Posted: Tue Jun 24, 2014 9:00 pm
by Simon
Hi Franco,
Here is what the gang came up with;
http://forums.livecode.com/viewtopic.php?f=7&t=20263
Some cool stuff there.

Simon

Re: random loop (avoid repetition)

Posted: Wed Jun 25, 2014 11:25 am
by francof
Simon wrote:Hi Franco,
Here is what the gang came up with;
http://forums.livecode.com/viewtopic.php?f=7&t=20263
Some cool stuff there.

Simon
ciao Simon,
many thanks, I did not see this topic. to do what I wanted I adapted the solution proposed by SparkOut (last but not least)....
I will post the adapted code

franco

Re: random loop (avoid repetition)

Posted: Wed Jun 25, 2014 4:58 pm
by francof
ok, I have finished the random steps :!:

Code: Select all

 put the hilitedbutton of grp "btnLivelli" into tNumLiv
    
    put getDatabaseID() into tDatabaseID
    put "SELECT DOMANDA, NUM from TBLDOMANDE_VARIE WHERE LIVELLO  = '"& tNumLiv &"' " into tSQL
    put revDataFromQuery(tab,return,tDatabaseID,tSQL) into tRecords
    
     sort lines of tRecords by random(the number of lines of tRecords)     --"mescola" tutte le dom del liv. selez.
     put 1 into tCont                                 --imposta il contatore a 1 per visualizzare la prima dom casuale
set ItemDel to TAB
   if tCont <= (the num of lines of tRecords) then                                 
      put item 1 of line tCont of tRecords  into field "txtDomanda"
      put item 2 of line tCont of tRecords  into tNumDom
      put tCont + 1 into tCont                      --incrementa il contatore per visualizzare la dom successiva
      put getDatabaseID() into tDatabaseID
      put "SELECT RISPOSTA from TBLDOMANDE_VARIE WHERE NUM  = '"& tNumDom &"' " into tSQL
      put revDataFromQuery(tab,return,tDatabaseID,tSQL) into field "txtRisposta"
   else
       set the disabled of button btnDomSuccessiva to true --raggiunta la fine delle domande disabilita il tasto domanda successiva 
       answer "Sono state poste tutte le domande del livello. Ripetere la selezione del livello o premere il tasto Indietro per uscire" with "Ok"
   end if
certainly the code can be optimized, however it works.

by
franco

Re: random loop (avoid repetition)

Posted: Wed Jun 25, 2014 8:07 pm
by phaworth
Another alternative might be to have SQL deal with all this for you. You'd have to add a column to your table - QuestionSelected TEXT.

At the start of processing your questions ( or the end):

UPDATE TBLDOMANDE_VARIE SET QuestionSelected to "false"

Your SELECT Statement would then be:

SELECT rowid,DOMANDA, NUM from TBLDOMANDE_VARIE WHERE QuestionSelected='false' LIVELLO = '"& tNumLiv &"' " & "LIMIT 1"
UPDATE TBLDOMANDE_VARIE SET QuestionSelected='true' WHERE rowid=<the selected rowid>

Not sure if there are any advantages to doing it that way.

Pete

Re: random loop (avoid repetition)

Posted: Thu Jun 26, 2014 2:11 pm
by francof
Hi Pete,
thanks for your suggestion, I will take note of it if were to serve me in future.

franco