nback-task Array with fixed number of matches

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

JamesWebster
Posts: 25
Joined: Sun Aug 07, 2011 11:36 am

nback-task Array with fixed number of matches

Post by JamesWebster » Sat Aug 13, 2011 1:33 am

I'm trying to create an array which fullfills some specifics it's supposed to contain a certain number of letters (Varible: totallaenge).
While the first few letters in the array <= xback can be completely random it's different for the rest. There's supposed to be a certain number of matches between within the array, e.g. if xback is 1 and matches 3 and totallaenge 5 a possible sequence is (A a B b b) - I guess the problem with the code is somehow when I subtract 1 from x, but it makes sense for me, hopefully someone can help!
Thank you very much in advance!

Code: Select all

On Preopencard
   put "B C D F G H J K M N P Q R S T V X Z" into abc #start Values
   split abc with space
   put 1 into xback
   put 3 into matches 
   put 5 into totallaenge
   put 0 into gematched

   repeat with x = 1 to totallaenge
      put Random(17) into randomnumber 
      If x <= xback then 
         put abc[randomnumber] into nback[x]
      else
         put Random(20) into muenze
         If gematched <= matches And muenze = 10 then
            put ToLower(nback[x-xback]) into nback[x]
            add 1 to gematched
         Else
            If abc[randomnumber] <> nback[x-xback] then
               put abc[randomnumber] into nback[x]
            else
               subtract 1 from x
            End if
         End if
         End if
                     put Test && nback[x] into Test
   end repeat

   put Test into fld "letter"
   delete local Test
end Preopencard
Last edited by JamesWebster on Sat Aug 13, 2011 3:51 pm, edited 5 times in total.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: nback-task Array with fixed number of matches

Post by Mark » Sat Aug 13, 2011 10:02 am

Hi,

What is it that you're trying to achieve and why? If I were going to put some random letters into another variable, I'd use a very simple script:

Code: Select all

put "BCDFGHJKMNPQRSTVXZ" into myList
repeat myLaenge
  put any char of myList into myChar
  if any item of "true,false" then
    put toLower(myChar) into myChar
  end if
  put myChar after myNewList
  -- put space after myNewList if you need this
end repeat
Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

JamesWebster
Posts: 25
Joined: Sun Aug 07, 2011 11:36 am

Re: nback-task Array with fixed number of matches

Post by JamesWebster » Sat Aug 13, 2011 11:15 am

I know it's kind of complicated. Basically you're right: I need random letters, which randomly are LowerCase Or UpperCase in a list. Wikipedia describes it as follows:
The subject is presented with a sequence of stimuli, and the task consists of indicating when the current stimulus matches the one from n steps earlier in the sequence. The load factor n can be adjusted to make the task more or less difficult.
For example, an auditory three-back test could consist of the experimenter reading the following list of letters to the test subject:
T L H C H S C C Q L C K L H C Q T R R K C H R
The subject is supposed to indicate when the letters marked in bold are read, because those correspond to the letters that were read three steps earlier.
I'm trying to create it's visual counterpart with some specifications. I need a given amount of matches so I can compare the users results. That's what I don't know how to do or to be more accurate I don't know why the code doesn't work.

Edit: I just noticed something. The real challenge is creating a list of random numbers, which has a certain length. Most important with this is that they have to be different from each other. Using the list after it's sorted from smallest to biggest number, I can just fill the nback array while checking if nback[x] is in the random number Array.
I guess I solved it:
1) Create Array with a certain length - fill it with the numbers from 1 to certain length
2) repeat with x =1 to totallength
put Random(number) into z
put array[x] into temp
put array[z] into array[x]
put temp into array[z]
end repeat

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: nback-task Array with fixed number of matches

Post by bn » Sat Aug 13, 2011 1:42 pm

Hi James,

I still don't fully understand what your conditions are.

Could you explain a little more?

I gave it a try. It is awkward and if I would better understand the problem the code could probably made easier.

This gives you match instances of a repeated letter. What I did not account for is the muenze condition when deciding that a new letter is the same as letter xback.
Do you really want muenze (= coin) to be exactly 10 or do you want to test for muenze to be either smaller or larger than 10 representing the two sides of muenze?
You have 18 letters in abc but you use only 17 (random 17). Poor Z is never used.

I changed the repeat loop from x = 1 to z and made my own counter. This is not necessary the way it is now.

Code: Select all

On mouseUp
   put "B C D F G H J K M N P Q R S T V X Z" into abc #start Values
   split abc with space
   put 1 into xback
   put 3 into matches 
   put 5 into totallaenge
   put 0 into gematched
   put 0 into tCounter
   put "" into Test
   
   repeat until tCounter =totallaenge
      add 1 to tCounter
      put Random(17) into randomnumber 
      If tCounter <= xback then 
         put abc[randomnumber] into nback[tCounter]
      else
         put Random(20) into muenze
         If gematched < matches And muenze = 10 then
            put ToLower(nback[tCounter-xback]) into nback[tCounter]
            add 1 to gematched
         Else
            If abc[randomnumber] <> nback[tCounter-xback] then
               put abc[randomnumber] into nback[tCounter]
            else
               -- we use this as a match if < gematched, you could add the condition muenze = 10 here also
               if gematched < matches then
                  put toLower(nback[tCounter-xback]) into nback[tCounter]
                  add 1 to gematched
               else
                  -- enough matches found lets generate a new random letter
                  repeat
                     put abc[random(17)] into tRandomLetter
                     if tRandomLetter <> nback[tCounter-xback] then
                        put tRandomLetter into nback[tCounter]
                        exit repeat
                     end if
                  end repeat
                  
               end if
            End if
         End if
      End if
      put nback[tCounter] & space after Test
      
   end repeat
   
   put Test into fld "letter"
end mouseUp
kind regards

Bernd

JamesWebster
Posts: 25
Joined: Sun Aug 07, 2011 11:36 am

nback-task Array with fixed number of matches

Post by JamesWebster » Sat Aug 13, 2011 2:55 pm

Hi Bernd,

I've solved how to create an array of random numbers without duplicates and to order the first few characters of it.
If totalleange is 45 an example for the array of random numbers might be (12,17, 34, 45) The length of the random number array I use is determined by the amount of matches. Now I'm struggling on how to use this to fill the nback array with random letters except for the postions in the random numbers array. These positions have to be filled with the letter which is xback (The following code is the time consuming mess I'm dealing with right now) Don't know if it got any clearer what I'm trying to accomplish :?:

Edit: I think I solved a part of the problem. The random number list began with 1 instead of xback + 1 which is a problem since you can't have a match for 1 to xback. But something is still wrong the number of matches isn't always correct...

Code: Select all

     repeat with x = 1 to totallaenge
      repeat with u = 1 to matches
         If arrays[u] = x then
            put "true" into temp
         End if
      end repeat
      If temp = "true" then
         put ToLower(nback[x-xback]) into nback[x]
         put "false" into temp
      Else
         put abc[Random(18)] into nback[x]
      end if
      put Test && nback[x] into Test
   end repeat
P.S.: Thank you for noticing the "17" mistake - I kind of overlooked that ;)

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: nback-task Array with fixed number of matches

Post by bn » Sat Aug 13, 2011 3:59 pm

Hi James,

I am afraid I lost you.

Did you try my code?

If you could describe all the conditions that you want in a structured way then I might understand (or not).

Kind regards

Bernd

JamesWebster
Posts: 25
Joined: Sun Aug 07, 2011 11:36 am

Re: nback-task Array with fixed number of matches

Post by JamesWebster » Sat Aug 13, 2011 4:30 pm

Hi Bernd,

Ok. I'll try:
1) I want a list of random letters in an array with a set length called totallaenge. Let's call the random number list rlist.
2) xback is the intervall which determines how many letters you go back, to look for matches (is it the same letter?)
e.g.: if xback is 1 and rlist is "NNCDC" NN would be considered a match.
other example: if xback is 2 and rlist is "NNCDC" CDC would be considered a match.
3) I want to create a nback with a set xback, a set number of matches and a set totallaenge.

That's all I'm trying to do. So far I've succesfully created a random numbers list. Lowest number within the random number list is xback + 1, highest number is the totallaenge of nback. This seems to work just fine (every time I've tried). What doesn't seem to work is putting the list to use. I want to use it to identify whenever the script should put in the letter which is xback within the array instead of a random letter.

Hopefully my explanation was a bit better this time.

with kind regards
James
Last edited by JamesWebster on Sun Jan 15, 2012 2:51 pm, edited 1 time in total.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: nback-task Array with fixed number of matches

Post by bn » Sat Aug 13, 2011 5:41 pm

Hi Fabian,

I made a version without arrays. Livecode is very good at handling chunks = parts of text etc.
Have a look whether it does what you are trying to do.

Code: Select all

on mouseUp
   put "BCDFGHJKMNPQRSTVXZ" into abc #start Values
   put 1 into xback
   put 3 into matches 
   put 18 into totallaenge
   put "" into tCollect
   put 0 into gematched
   
   repeat with i = 1 to totalLaenge
      -- just grab a random letter
      put char random(18) of abc after tCollect
      
      -- only test for xback if enough letters are in tCollect to compare
      if the number of chars of tCollect >=xback then
         
         -- test for xback here
         if (char (length (tCollect)- xback) of tCollect = (last char of tCollect))  then
            
            -- only if matches are not yet reached mark last character as lowercase
            if  (gematched < matches) then
               put toLower(last char of tCollect) into last char of tCollect
               add 1 to gematched
               
            else
               -- we don't want more xback occurences and get a new random letter that is not the one used before              
               repeat 
                  put char random(18) of abc into tAChar
                  if tAChar <> last char of tCollect then
                     put tAChar into last char of tCollect
                     -- we found a new random letter, get out of repeat loop
                     exit repeat
                  end if
               end repeat
            end if
         end if
      end if
   end repeat
   
   -- lets put spaces between the letters
   repeat for each char aChar in tCollect
      put aChar & space after tOutPut
   end repeat
   delete last char of tOutPut -- a space
   
   put tOutPut into field "letter"
   -- put gematched into field 2 -- if you want to monitor how many matches make a field
end mouseUp
kind regards

Bernd

JamesWebster
Posts: 25
Joined: Sun Aug 07, 2011 11:36 am

Re: nback-task Array with fixed number of matches

Post by JamesWebster » Sat Aug 13, 2011 7:09 pm

Hi Bernd,

thank you very much - I`m still kind of used to working with arrays. That makes things a bit easier.
But your Code doesn't work right. As far as I get your Code it puts every match letter to a Lower Case.
But changing the matched number doesn't produce an equal number of LowerCase letters, which should be the case.

kind regards
James

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: nback-task Array with fixed number of matches

Post by bn » Sat Aug 13, 2011 7:21 pm

Hi James,

I thought of match as a maximum number of matches i.e. if the random characters don't match then you easily have less then the max = matches number of occurences.

Do you want matches to be the number of times a match occurs?

Or am I misunderstanding your project again?

Could you post examples of what my code does not do right please, then it is easier for me to understand.
Just post the strings of letter produced by my code and indicate the settings. And indicate what in contrast at what setting you would like to see. You could correct the string manually to show the difference.

Kind regards

Bernd

JamesWebster
Posts: 25
Joined: Sun Aug 07, 2011 11:36 am

Re: nback-task Array with fixed number of matches

Post by JamesWebster » Sat Aug 13, 2011 7:33 pm

You're exactly right!, the amount of matches is should always occur, no more no less.
the setting:

Code: Select all

   put 1 into xback
   put 3 into matches 
   put 18 into totallaenge
   put "" into tCollect
   put 0 into gematched
the Output
P J Z J P N n F H P R V D M C G P R
N Q T M B Q D B X H V M Z D N K B R
D K S Z C S V v C c Z Q N J F Z F G
It's supposed to produce a set number of matches. In the first Output it produces one match instead of three.
In the second it doesn't produce any match. And in the third Output it produces two matches.[/size]
kind regard James

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: nback-task Array with fixed number of matches

Post by bn » Sat Aug 13, 2011 8:45 pm

Hi James,

I took the lazy way out: I let the random loop run until it has the right amount of matches. Otherwise it would not really be random and difficult to do with xback going from 1 to ... etc.
I tested it and it worked. But there might be combinations of totalLaenge, xback and matches that let it run for a long time or forever. (in that case press the escape key or command period on a Mac) I do test for the length and number of matches: if matches larger than half the length it probably cant finish and runs forever. More error checking probably needed.

Code: Select all

on mouseUp
   put "BCDFGHJKMNPQRSTVXZ" into abc #start Values
   put 1 into xback
   put 3 into matches 
   put 6 into totallaenge
   put "" into tCollect
   put 0 into gematched
   
   -- just a little error checking, more is probably needed
   -- matches should not be more than half the length
   if matches > totalLaenge/2 then 
      answer "can not fit matches into length"
      exit mouseUp
   end if
   
   repeat until gematched = matches  -- just let the routine run until it has found a combination of matches that  matches
      
      repeat with i = 1 to totalLaenge
         -- just grab a random letter
         put char random(18) of abc after tCollect
         
         -- only test for xback if enough letters are in tCollect to compare
         if the number of chars of tCollect >xback then
            
            -- test for xback here
            if (char (length (tCollect)- xback) of tCollect = (last char of tCollect))  then
               
               -- only if matches are not yet reached mark last character as lowercase
               if  (gematched < matches) then
                  put toLower(last char of tCollect) into last char of tCollect
                  add 1 to gematched
                  
               else
                  -- we don't want more xback occurences and get a new random letter that is not the one used before
                  repeat 
                     put char random(18) of abc into tAChar
                     if tAChar <> last char of tCollect then
                        put tAChar into last char of tCollect
                        -- we found a new random letter, get out of repeat loop
                        exit repeat
                     end if
                  end repeat
               end if
            end if
         end if
      end repeat
      
      if gematched < matches then -- not enough matches repeat the whole thing
         put 0 into gematched
         put "" into tCollect
      end if
      
      wait 0 milliseconds with messages -- in case the repeat loop can not finish we can interrupt it with command + period or escape
      
   end repeat
   
   -- lets put spaces between the letters
   repeat for each char aChar in tCollect
      put aChar & space after tOutPut
   end repeat
   delete last char of tOutPut -- a space
   
   put tOutPut into field "letter"
   --put gematched into field 2 -- if you want to monitor how many matches make a field
   --put xback into field 3
end mouseUp
How does this work for you?

Kind regards

Bernd

JamesWebster
Posts: 25
Joined: Sun Aug 07, 2011 11:36 am

Re: nback-task Array with fixed number of matches

Post by JamesWebster » Sun Aug 14, 2011 8:11 am

Hi Bernd,

thank you very much. This works just fine. Now it's the next stage of the project which troubles me at one point.
I've modified your script so the Lower case isn't assigned to a match any more and replaced the handler with preopencard.

Code: Select all

split tOutput with space
put 0 into aktuell
put 0 into Fehleranzahl
   nbacktiming
end Preopencard

#User Input 
On MouseUp
   put "match" into Antwort
end MouseUp
On MouseDoubleUp
   put "mismatch" into Antwort
end MouseDoubleUp

On nbacktiming
  global durchgang # used for a later stage
   put (aktuell & "- Fehlerzahl: " & Fehleranzahl[durchgang]) into field id 2151
   If aktuell <= totallaenge then
      If Random(8) is 1 then
         put ToLower(tOutput[aktuell]) into field id 2143
      Else
         put tOutput[aktuell] into field id 2143
      end if

      #Check user Input, appears to work just fine
      If aktuell > xback then
         If (tOutput[aktuell] is tOutput[aktuell-xback]) then
            If (Antwort is "mismatch") then
               Mouseup
               MouseDoubleup
               add 1 to Fehleranzahl[durchgang]
            End if
         Else
            If (Antwort is "match") then
               Mouseup
               MouseDoubleup
               add 1 to Fehleranzahl[durchgang]
            End if
         End if
         If (Antwort is "missed") then
            add 1 to Fehleranzahl[durchgang]
         End if
         put "missed" into Antwort
         
      End if
      
[b]      Wait for 500 millisec
      
      add 1 to aktuell
      put "" into field id 2143
      send "nbacktiming" to me in 2500 millisec[/b] # this doesn't seem to work, the letter are shown to fast
   Else
      delete local aktuell
add 1 to durchgang
      go to next card
   End if
end nbacktiming

JamesWebster
Posts: 25
Joined: Sun Aug 07, 2011 11:36 am

Re: nback-task Array with fixed number of matches

Post by JamesWebster » Sun Aug 14, 2011 8:11 am

Hi Bernd,

thank you very much. This works just fine. Now it's the next stage of the project which troubles me at one point.
I've modified your script so the Lower case isn't assigned to a match any more and replaced the handler with preopencard.

Code: Select all

split tOutput with space
put 0 into aktuell
put 0 into Fehleranzahl
   nbacktiming
end Preopencard

#User Input 
On MouseUp
   put "match" into Antwort
end MouseUp
On MouseDoubleUp
   put "mismatch" into Antwort
end MouseDoubleUp

On nbacktiming
  global durchgang # used for a later stage
   put (aktuell & "- Fehlerzahl: " & Fehleranzahl[durchgang]) into field id 2151
   If aktuell <= totallaenge then
      If Random(8) is 1 then
         put ToLower(tOutput[aktuell]) into field id 2143
      Else
         put tOutput[aktuell] into field id 2143
      end if

      #Check user Input, appears to work just fine
      If aktuell > xback then
         If (tOutput[aktuell] is tOutput[aktuell-xback]) then
            If (Antwort is "mismatch") then
               Mouseup
               MouseDoubleup
               add 1 to Fehleranzahl[durchgang]
            End if
         Else
            If (Antwort is "match") then
               Mouseup
               MouseDoubleup
               add 1 to Fehleranzahl[durchgang]
            End if
         End if
         If (Antwort is "missed") then
            add 1 to Fehleranzahl[durchgang]
         End if
         put "missed" into Antwort
         
      End if
      
[b]      Wait for 500 millisec
      
      add 1 to aktuell
      put "" into field id 2143
      send "nbacktiming" to me in 2500 millisec[/b] # this doesn't seem to work, the letter are shown to fast
   Else
      delete local aktuell
add 1 to durchgang
      go to next card
   End if
end nbacktiming

JamesWebster
Posts: 25
Joined: Sun Aug 07, 2011 11:36 am

Re: nback-task Array with fixed number of matches

Post by JamesWebster » Sun Aug 14, 2011 8:11 am

Hi Bernd,

thank you very much. This works just fine. Now it's the next stage of the project which troubles me at one point.
I've modified your script so the Lower case isn't assigned to a match any more and replaced the handler with preopencard.
The problem at this point is that it doesn't show the letter for 500 milliseconds, puts empty into field and waits for
another 2,5 s before showing the next letter. The letters appear to fast. (Oh and I'm still working with arrays,
didn't quite get the hang of using the characters - I've tried and failed miserably)

Code: Select all

split tOutput with space
put 0 into aktuell
put 0 into Fehleranzahl
   nbacktiming
end Preopencard

#User Input 
On MouseUp
   put "match" into Antwort
end MouseUp
On MouseDoubleUp
   put "mismatch" into Antwort
end MouseDoubleUp

On nbacktiming
  global durchgang # used for a later stage
   put (aktuell & "- Fehlerzahl: " & Fehleranzahl[durchgang]) into field id 2151
   If aktuell <= totallaenge then
      If Random(8) is 1 then
         put ToLower(tOutput[aktuell]) into field id 2143
      Else
         put tOutput[aktuell] into field id 2143
      end if

      #Check user Input, appears to work just fine
      If aktuell > xback then
         If (tOutput[aktuell] is tOutput[aktuell-xback]) then
            If (Antwort is "mismatch") then
               Mouseup
               MouseDoubleup
               add 1 to Fehleranzahl[durchgang]
            End if
         Else
            If (Antwort is "match") then
               Mouseup
               MouseDoubleup
               add 1 to Fehleranzahl[durchgang]
            End if
         End if
         If (Antwort is "missed") then
            add 1 to Fehleranzahl[durchgang]
         End if
         put "missed" into Antwort
         
      End if
      
[b]      Wait for 500 millisec
      
      add 1 to aktuell
      put "" into field id 2143
      send "nbacktiming" to me in 2500 millisec[/b] # this doesn't seem to work, the letter are shown to fast
   Else
      delete local aktuell
add 1 to durchgang
      go to next card
   End if
end nbacktiming

Post Reply