random loop (avoid repetition)

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
francof
Posts: 237
Joined: Fri Apr 11, 2014 10:51 am

random loop (avoid repetition)

Post by francof » Tue Jun 24, 2014 8:47 pm

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: random loop (avoid repetition)

Post by Simon » Tue Jun 24, 2014 9:00 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

francof
Posts: 237
Joined: Fri Apr 11, 2014 10:51 am

Re: random loop (avoid repetition)

Post by francof » Wed Jun 25, 2014 11:25 am

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

francof
Posts: 237
Joined: Fri Apr 11, 2014 10:51 am

Re: random loop (avoid repetition)

Post by francof » Wed Jun 25, 2014 4:58 pm

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

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: random loop (avoid repetition)

Post by phaworth » Wed Jun 25, 2014 8:07 pm

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

francof
Posts: 237
Joined: Fri Apr 11, 2014 10:51 am

Re: random loop (avoid repetition)

Post by francof » Thu Jun 26, 2014 2:11 pm

Hi Pete,
thanks for your suggestion, I will take note of it if were to serve me in future.

franco

Post Reply