two cards that open randomly
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Re: two cards that open randomly
Hi AtoZ
This is what I have done, I can't work out why the random card choice doesn't stop when it gets to 20? Any ideas?
Many thanks
HJay
This is what I have done, I can't work out why the random card choice doesn't stop when it gets to 20? Any ideas?
Many thanks
HJay
- Attachments
-
- Matching Numbers (2).zip
- (4.6 KiB) Downloaded 227 times
Re: two cards that open randomly
hmmm don't think I have done that right. Am I just getting the first random FR1 or FR20 on each new mouseup?
Re: two cards that open randomly
HJay,
It only seems like magic if you don't know what the man behind the curtain is doing. (Smile.) If there is some part of my code that you are having trouble following, please ask and I'll try to explain.
To get back to your original question of why you kept getting the same result every time you ran the handler. I believe it has to do with the fact that the LiveCode does not generate true random numbers. (This is true also of all other modern programming languages that I am aware of.) Computers are deterministic, and it's actually quite hard (some say impossible) for a computer to genereate true random numbers.
Instead, LiveCode uses a psuedo-random number generator that uses an algorithm to generate a string of apparently random numbers. The things that shocks most people new to programming is if you call the random function repeated in a handler, it generates exactly the same sequence of numbers each time. Developers try to pass this off as a "feature," and admittedly there are some times when this is useful -- for scientists, statistictians, and even programmers who are trying to debug a program and want to run the same series of random numbers over and over again while they tinker with other parts of the program to get it working right. But for most of us, it's a pain in the neck to deal with, and when it's working right, the generated psuedo-random numbers look so convincing that it's easy to forget that they're not truly random.
So every time you opened your stack (or clicked on a button if you've moved the script there), LiveCode started to generate the same series of numbers that it used the last time. (It does reset itself each time you close and re-open LiveCode itself.) What to do?
Well, I'm sure, like everything else in LiveCode, that there are multiple ways of dealing with this. I use a two-step process:
1) Just before I begin the coding section to generate a "random" number, I insert the following line of code: set the randomSeed to the seconds This causes LiveCode to begin a new series of "random" numbers. You could set the randomSeed to almost any number, but using "the seconds" is a good choice since it is always a different number that the last time it was called (unless you call it again within one second -- and because LiveCode is so fast, you must be aware of this possibilty, in such a situtation you can set the randomSeed to the milliseconds instead. No code is likely to run twice within one millisecond).
2) I then generate all the random numbers I need in advance (using a repeat loop) and store them in a variable. Then I pull the random numbers from the variable as needed.
A few words about the last step. It would seem that instead of putting the numbers into a variable, you could just put them directly into a field (or wherever else you need them) as you generate them. However, for reasons that I've never seen clearly explained, the results you get doing this are not always reliable. This is not something new. Here for example is a quote from Rob Cozens from June 9, 2006: "Generally, if not always, evaluating the function into a variable and then referencing the variable works. I've never tracked down the "why"; but the fix [i.e., avoiding unpredicable results] seems to work reliably." (http://www.mail-archive.com/use-revolut ... 79081.html) This is one of those cases where I don't understand what the man behind the curtain is doing, but at least I know how to work around it. Perhaps those more knowledgable about LiveCode (which is almost everyone in these forums) can enlighted us further about these matters.
Dealing with random numbers is not as trivial or as easy as it first appears, and if you do a search on the Internet, you'll see that it has been the subject of countless discussions. Given the rather frequent need for random numbers in programs, I'm suprised that the LiveCode User Guide and Dictionary don't give a fuller account of how best to use the random function and how to avoid the sort of problem you've been having.
It only seems like magic if you don't know what the man behind the curtain is doing. (Smile.) If there is some part of my code that you are having trouble following, please ask and I'll try to explain.
To get back to your original question of why you kept getting the same result every time you ran the handler. I believe it has to do with the fact that the LiveCode does not generate true random numbers. (This is true also of all other modern programming languages that I am aware of.) Computers are deterministic, and it's actually quite hard (some say impossible) for a computer to genereate true random numbers.
Instead, LiveCode uses a psuedo-random number generator that uses an algorithm to generate a string of apparently random numbers. The things that shocks most people new to programming is if you call the random function repeated in a handler, it generates exactly the same sequence of numbers each time. Developers try to pass this off as a "feature," and admittedly there are some times when this is useful -- for scientists, statistictians, and even programmers who are trying to debug a program and want to run the same series of random numbers over and over again while they tinker with other parts of the program to get it working right. But for most of us, it's a pain in the neck to deal with, and when it's working right, the generated psuedo-random numbers look so convincing that it's easy to forget that they're not truly random.
So every time you opened your stack (or clicked on a button if you've moved the script there), LiveCode started to generate the same series of numbers that it used the last time. (It does reset itself each time you close and re-open LiveCode itself.) What to do?
Well, I'm sure, like everything else in LiveCode, that there are multiple ways of dealing with this. I use a two-step process:
1) Just before I begin the coding section to generate a "random" number, I insert the following line of code: set the randomSeed to the seconds This causes LiveCode to begin a new series of "random" numbers. You could set the randomSeed to almost any number, but using "the seconds" is a good choice since it is always a different number that the last time it was called (unless you call it again within one second -- and because LiveCode is so fast, you must be aware of this possibilty, in such a situtation you can set the randomSeed to the milliseconds instead. No code is likely to run twice within one millisecond).
2) I then generate all the random numbers I need in advance (using a repeat loop) and store them in a variable. Then I pull the random numbers from the variable as needed.
A few words about the last step. It would seem that instead of putting the numbers into a variable, you could just put them directly into a field (or wherever else you need them) as you generate them. However, for reasons that I've never seen clearly explained, the results you get doing this are not always reliable. This is not something new. Here for example is a quote from Rob Cozens from June 9, 2006: "Generally, if not always, evaluating the function into a variable and then referencing the variable works. I've never tracked down the "why"; but the fix [i.e., avoiding unpredicable results] seems to work reliably." (http://www.mail-archive.com/use-revolut ... 79081.html) This is one of those cases where I don't understand what the man behind the curtain is doing, but at least I know how to work around it. Perhaps those more knowledgable about LiveCode (which is almost everyone in these forums) can enlighted us further about these matters.
Dealing with random numbers is not as trivial or as easy as it first appears, and if you do a search on the Internet, you'll see that it has been the subject of countless discussions. Given the rather frequent need for random numbers in programs, I'm suprised that the LiveCode User Guide and Dictionary don't give a fuller account of how best to use the random function and how to avoid the sort of problem you've been having.
Re: two cards that open randomly
Hi AtoZ
I am trying to peep round the curtain but the man around the back seems to keep hiding himself behind another curtain.
It’s a good job I am persistent.
After lots of clicking very slowly through the variable watcher what was happening slowly dawned on me. The tBucket variable confused me at first, then made complete sense, then made me laugh at myself and wonder how it had confused me in the first place.
I had heard this about computers having a problem with random but hadn’t realised the problem was so marked, i.e. getting the same set of random numbers twice! That would really cause me problems in this software.
So if I use set the randomSeed to the seconds in the line preceding any random command will this work?
My main problem was I think that I put your code in the wrong place, I have put it in the start button but I think that because I am asking it to go away and do something on another card and then return back to the start button, I think if may be starting afresh every time rather than running thought the code, which I think is why it does not stop at twenty. There are way too many ‘I thinks’ in that paragraph sorry
Would you mind having a look at the zip file for me please?
Many many many thanks
HJay
I am trying to peep round the curtain but the man around the back seems to keep hiding himself behind another curtain.


After lots of clicking very slowly through the variable watcher what was happening slowly dawned on me. The tBucket variable confused me at first, then made complete sense, then made me laugh at myself and wonder how it had confused me in the first place.

I had heard this about computers having a problem with random but hadn’t realised the problem was so marked, i.e. getting the same set of random numbers twice! That would really cause me problems in this software.
So if I use set the randomSeed to the seconds in the line preceding any random command will this work?
My main problem was I think that I put your code in the wrong place, I have put it in the start button but I think that because I am asking it to go away and do something on another card and then return back to the start button, I think if may be starting afresh every time rather than running thought the code, which I think is why it does not stop at twenty. There are way too many ‘I thinks’ in that paragraph sorry
Would you mind having a look at the zip file for me please?
Many many many thanks
HJay
Re: two cards that open randomly
I'm sorry but our posts keep crossing each other. Once again, my previous long post was in reference to your initial stated problem.
With regard to your latest scripts, you need to do a little more to adapt what I showed in my test stack. Specifically, move most of the code to the card script in an "onOpenCard" handler. It should look something like this:
You just need to create the random list of card names once for every 20 trips to your cards, so that's why you don't want it in the button script. You'll note that I've added two things: a line near the top to set the randomSeed, and a new global called gNextCard. The new global is needed because the code is now split between the card script and the script for the Start button, and we need to be able to access the variable from both scripts. (The variable is filled in the openCard script, and then emptied it in the button script.)
Then the only script you need for the Start button is as follows:
On the following cards, you'll have to add similar code to the buttons on them to take (and delete) another item from the gNextCard variable (don't forget to declare the global at the top of each script).
Clicking on these buttons will work for 20 clicks. You'll probably want to keep track of that. One way would be to add some code each time an item is deleted from gNextCard, that checks to see how many items are still in the variable. If the answer is 0, then go to the finish card.
Hope this helps.
P.S. In looking at your stack, I'm not really sure of the function of the cards with the colored backgrounds. And, as a user, when one of these cards open, I'm confused because I have a choice of two buttons to click, but I have no idea why I should choose one over the other. Perhaps you could explain the cards to me. Thanks.
With regard to your latest scripts, you need to do a little more to adapt what I showed in my test stack. Specifically, move most of the code to the card script in an "onOpenCard" handler. It should look something like this:
Code: Select all
global gNextCard
local tx, tBucket, tChosen
on OpenCard
put empty into gNextCard
put empty into tBucket
set randomSeed to the seconds
repeat with tx = 1 to 10
put "FR1," after tBucket
put "FR20," after tBucket
end repeat
repeat with tx = 1 to 20
put random(number of items of tBucket) into tChosen
put item tChosen of tBucket & comma after gNextCard
delete item tChosen of tBucket
end repeat
end OpenCard
Then the only script you need for the Start button is as follows:
Code: Select all
global gNextCard
on mouseUp
go to card item 1 of gNextCard
delete item 1 of gNextCard
end mouseUp
Clicking on these buttons will work for 20 clicks. You'll probably want to keep track of that. One way would be to add some code each time an item is deleted from gNextCard, that checks to see how many items are still in the variable. If the answer is 0, then go to the finish card.
Hope this helps.
P.S. In looking at your stack, I'm not really sure of the function of the cards with the colored backgrounds. And, as a user, when one of these cards open, I'm confused because I have a choice of two buttons to click, but I have no idea why I should choose one over the other. Perhaps you could explain the cards to me. Thanks.
Re: two cards that open randomly
Hi AtoZ
When you say
at the top of every button script and
at the end of every button script?
i.e.
I haven’t tried it as I’m a bit scarred of making a mess and not being able to get back to where I was
The cards with the coloured backgrounds: this whole program is an attempt at a replication of an experiment that showed that having a colour that was correlated with hard work (doing 20 matching tasks) was selected over a colour that was correlated with less work (doing 1 matching task). The idea is to find an intervention that makes learning disabled pupils choose to do harder work themselves rather than someone saying they have to. That is the theory anyway. But first I have to replicate the initial findings of a paper in a journal. I initially tried to do all this on cards, but after doing a bit of maths I realised I was going to have to make and cut out 10,000 individual cards that could only be used once. Hence arriving here and attempting to learn LiveCode. So when LiveCode makes my brain hurt I just have to think of cutting out 10,000 cards.
I am very much appreciating your help
HJay
When you say
Do you mean that I need to putOn the following cards, you'll have to add similar code to the buttons on them to take (and delete) another item from the gNextCard variable (don't forget to declare the global at the top of each script).
Code: Select all
global gNextCard
Code: Select all
go to card item 1 of gNextCard
delete item 1 of gNextCard
i.e.
Code: Select all
global gCorrect,gWrong,gRightg,NextCard
on mouseUp
if the label of me = the label of button "testNumber" then
add 1 to gCorrect
else
add 1 to gWrong
end if
if the label of me = the label of button "testNumber" then
add 1 to gRight
else
Put 0 into gRight
end if
setTheTest
go to card item 1 of gNextCard
delete item 1 of gNextCard
end mouseUp

The cards with the coloured backgrounds: this whole program is an attempt at a replication of an experiment that showed that having a colour that was correlated with hard work (doing 20 matching tasks) was selected over a colour that was correlated with less work (doing 1 matching task). The idea is to find an intervention that makes learning disabled pupils choose to do harder work themselves rather than someone saying they have to. That is the theory anyway. But first I have to replicate the initial findings of a paper in a journal. I initially tried to do all this on cards, but after doing a bit of maths I realised I was going to have to make and cut out 10,000 individual cards that could only be used once. Hence arriving here and attempting to learn LiveCode. So when LiveCode makes my brain hurt I just have to think of cutting out 10,000 cards.
I am very much appreciating your help
HJay
Re: two cards that open randomly
HJay,
First, let me tell you how I deal with fear of messing up a perfectly fine script and not being able to get back to where I was. (Please bear with me here, this is one of those situations where it's takes a lot of words to explain what is actually a quite simple process.)
What I do is just make a backup of my stack every time I decide to embark on a major new part of my script (in fact I try to remember to make backups more frequently than that). It's just a a two step process after you create a folder inside your project folder named "Backups":
1a. (With your stack open) Choose "Save As..." from the File menu,
1b. Add the date and time to the front of your file name, and
1c. Store it in the Backup folder.
For example, if I were doing that now with your stack, I would save it as "04-19-2012 2-04 pm Matching Numbers.livecode" I include the time because when I'm hard at work on the stack, I might make several backup stacks in the same day. Also note that I use dashes in both the date and time to avoid "/" and ":" which are no-nos in file names.
2. THIS IS IMPORTANT: You must close your new backup stack, and re-open your original stack.
If you don't, you'll be making changes to the backup rather than the original. (It's not the end of the world if you do that, when you realize it, you just have to switch the names of the two stacks, being careful to do that in separate folders so you don't rewrite one on the other.)
Having made the backup, I can then proceed to radically change my stack, knowing that if I get lost or decide to abandon my new approach, all I have to do is open my latest backup, use "Save As..." to save it back to my original name and location, and all will be as it was before I began my changes.
Of course, some people are lucky enough to have programs or systems that make automatic backups for them (although all too often I've seen problems with this, either with backups not being made when you need them, or difficulties in restoring & opening the backup).
As an additional safeguard you might also make periodic backups to an external hard drive or flash drive in case your computer crashes. Never a bad idea.
-----
OK, back to your questions:
Yes, you understand what I suggested perfectly. You need to add that code to every button the user might click on that you want to count as part of your 20 cards viewings. In the third piece of code that you posted, the only problem I saw, was that you left the "g" off the front of NextCard when you declared in the the globals. (Do you understand why I put a "g" in front of every global variable, and a "t" in front of every local variable?)
I haven't focused yet on how you plan to use the variables gCorrect and gWrong. Hopefully they're working the way you want already. If you have questions about this or anything else in the stack, ask away.
P.S. Thanks for the info on the cards with colored backgrounds. Not sure I understand yet how they will be used in your stack, but I don't need to as long as you have them working the way you want.
First, let me tell you how I deal with fear of messing up a perfectly fine script and not being able to get back to where I was. (Please bear with me here, this is one of those situations where it's takes a lot of words to explain what is actually a quite simple process.)
What I do is just make a backup of my stack every time I decide to embark on a major new part of my script (in fact I try to remember to make backups more frequently than that). It's just a a two step process after you create a folder inside your project folder named "Backups":
1a. (With your stack open) Choose "Save As..." from the File menu,
1b. Add the date and time to the front of your file name, and
1c. Store it in the Backup folder.
For example, if I were doing that now with your stack, I would save it as "04-19-2012 2-04 pm Matching Numbers.livecode" I include the time because when I'm hard at work on the stack, I might make several backup stacks in the same day. Also note that I use dashes in both the date and time to avoid "/" and ":" which are no-nos in file names.
2. THIS IS IMPORTANT: You must close your new backup stack, and re-open your original stack.
If you don't, you'll be making changes to the backup rather than the original. (It's not the end of the world if you do that, when you realize it, you just have to switch the names of the two stacks, being careful to do that in separate folders so you don't rewrite one on the other.)
Having made the backup, I can then proceed to radically change my stack, knowing that if I get lost or decide to abandon my new approach, all I have to do is open my latest backup, use "Save As..." to save it back to my original name and location, and all will be as it was before I began my changes.
Of course, some people are lucky enough to have programs or systems that make automatic backups for them (although all too often I've seen problems with this, either with backups not being made when you need them, or difficulties in restoring & opening the backup).
As an additional safeguard you might also make periodic backups to an external hard drive or flash drive in case your computer crashes. Never a bad idea.
-----
OK, back to your questions:
Yes, you understand what I suggested perfectly. You need to add that code to every button the user might click on that you want to count as part of your 20 cards viewings. In the third piece of code that you posted, the only problem I saw, was that you left the "g" off the front of NextCard when you declared in the the globals. (Do you understand why I put a "g" in front of every global variable, and a "t" in front of every local variable?)
I haven't focused yet on how you plan to use the variables gCorrect and gWrong. Hopefully they're working the way you want already. If you have questions about this or anything else in the stack, ask away.
P.S. Thanks for the info on the cards with colored backgrounds. Not sure I understand yet how they will be used in your stack, but I don't need to as long as you have them working the way you want.
Re: two cards that open randomly
HJay,
After I sent my last message, as I was driving to a friend's, I remembered that you still need to add a little code to your script to trap when the user has made all twenty visits to your two cards. In an earlier post I suggested that you could add another global variable and keep track of the status there. But as was driving along it occurred to me that it isn't necessary to add the variable. Instead I would suggest you modify the script you are adding at the end of each button to read as follows:
Strictly speaking you don't need this test at the end of the Start button (because gNextCard will always have 20 items at the end of the Start button script), but it wouldn't hurt if it was there.
After I sent my last message, as I was driving to a friend's, I remembered that you still need to add a little code to your script to trap when the user has made all twenty visits to your two cards. In an earlier post I suggested that you could add another global variable and keep track of the status there. But as was driving along it occurred to me that it isn't necessary to add the variable. Instead I would suggest you modify the script you are adding at the end of each button to read as follows:
Code: Select all
if the number of items in gNextCard = 0 then
go to card "Finished"
else
go to card item 1 of gNextCard
delete item 1 of gNextCard
end if
Re: two cards that open randomly
Often when I'm only changing a few handlers, I just duplicate them in the script and comment out the originals. Then if my changes go haywire I can delete them and uncomment the originals. It's faster than finding a backup on disk. This works pretty well with small incremental changes.First, let me tell you how I deal with fear of messing up a perfectly fine script and not being able to get back to where I was.
If I'm changing a lot of stuff, or my changes are to the objects themselves rather than their scripts, then I do the backup thing. The easiest way for me to do that is to go to the Finder (or Windows Explorer) and make a duplicate there. That way I can continue working on the stack that's already open, and the duplicate on disk becomes the one I can revert to. It's easier than messing around with how LiveCode treats "save as" files. On a Mac you just need to click over to Finder, option-drag the stack you've got open, and then click back to LiveCode.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: two cards that open randomly
Hi AtoZ
Am having a little trouble Have got the code on the card and the other bit of code on the start button also have placed the code on the coloured buttons as the user is always directed from the ‘FR1 white’ to ‘Yellow or red ’ or ‘FR20 white’ to ‘blue or green’. Have attached a picture that I hope will make a bit more sense?
I have checked and double checked and spent ages in the variable watcher but I can’t get anything to happen when the start button is clicked
I now have backups though thank you for the detailed instructions.
The accidentally left out g was just an accident, I know that t stands for temporary and g for global and I know that global works outside of buttons etc. when it is declared. Have also realised that lots of people don’t use the t, which is why it isn’t in some of my scripts as other people have helped along the way. When I get a chance I will go through and put them all in.
I have gCorrect and gwrong working as I want at the moment; they are only temporary whilst I work out what goes where.
So my problem at the moment is to get the program to progress past the start button, have attached what I have done, if you wouldn’t mind having a little look (can't add a second attachment to this post will add in below)
Many many thanks
HJay
Am having a little trouble Have got the code on the card and the other bit of code on the start button also have placed the code on the coloured buttons as the user is always directed from the ‘FR1 white’ to ‘Yellow or red ’ or ‘FR20 white’ to ‘blue or green’. Have attached a picture that I hope will make a bit more sense?
I have checked and double checked and spent ages in the variable watcher but I can’t get anything to happen when the start button is clicked
I now have backups though thank you for the detailed instructions.
The accidentally left out g was just an accident, I know that t stands for temporary and g for global and I know that global works outside of buttons etc. when it is declared. Have also realised that lots of people don’t use the t, which is why it isn’t in some of my scripts as other people have helped along the way. When I get a chance I will go through and put them all in.
I have gCorrect and gwrong working as I want at the moment; they are only temporary whilst I work out what goes where.
So my problem at the moment is to get the program to progress past the start button, have attached what I have done, if you wouldn’t mind having a little look (can't add a second attachment to this post will add in below)
Many many thanks
HJay
Re: two cards that open randomly
Here is the program
- Attachments
-
- Matching Numbers 23-4-12.zip
- (5.07 KiB) Downloaded 261 times
Re: two cards that open randomly
Hi jaque
Have found commenting and uncommenting invaluable. Am using a PC I like your idea of using windows explorer but my only problem with this is that my files only seem to be there when I go to open a stack within LiveCode and when I navigate to the file location with windows explorer they are nowhere to be seen? I have ‘show hidden files set’ so am a bit confused as to where LiveCode is hiding them?
HJay
Have found commenting and uncommenting invaluable. Am using a PC I like your idea of using windows explorer but my only problem with this is that my files only seem to be there when I go to open a stack within LiveCode and when I navigate to the file location with windows explorer they are nowhere to be seen? I have ‘show hidden files set’ so am a bit confused as to where LiveCode is hiding them?
HJay
Re: two cards that open randomly
What location are you navigating to? Where did you save the file in the first place?I navigate to the file location with windows explorer
Try this: in LiveCode with your stack loaded into memory, type this into the message box
Code: Select all
put the long id of the topstack
Re: two cards that open randomly
Hi mwieder
I open LiveCode and click File > Open Stack
I presume this is the default location? The file location appears as:-
Computer > Local Disk (C:) > Program Files > RunRev > LiveCode 5.5 > Matching Numbers
If I into the message box I get
stack "C:/Program Files/RunRev/LiveCode 5.5/Matching Numbers.livecode"
But if I navigate there by clicking on Computer > Local Disk (C:) > Program Files > RunRev > LiveCode 5.5 > Matching Numbers
Then this is all I can find?
HJay
I open LiveCode and click File > Open Stack
I presume this is the default location? The file location appears as:-
Computer > Local Disk (C:) > Program Files > RunRev > LiveCode 5.5 > Matching Numbers
If I
Code: Select all
put the long id of the topstack
stack "C:/Program Files/RunRev/LiveCode 5.5/Matching Numbers.livecode"
But if I navigate there by clicking on Computer > Local Disk (C:) > Program Files > RunRev > LiveCode 5.5 > Matching Numbers
Then this is all I can find?
HJay
Re: two cards that open randomly
Yes - this is poor coding on the part of runrev, but we're stuck with it.I presume this is the default location?
This is a Bad Idea. I realize it's the default location, but even so. It was a bad idea even before Windows 7 made it a worse idea.stack "C:/Program Files/RunRev/LiveCode 5.5/Matching Numbers.livecode"
Depending on user permissions, you may or may not be able to store files into the "Program Files" directory hierarchy.
Better to store the stacks somewhere in the Documents directory.
However... hmmm... your stack file *doesn't* seem to be there.
Try a Windows search for "Matching Numbers" and see what turns up.