Random Loops
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 7
- Joined: Sat Jun 22, 2019 9:19 pm
Random Loops
Is it possible to loop through a list of numbers for a specified time? For example, I have a field with the numbers 1 to 20 in a list, and I want to be able to randomly pick numbers for 3 seconds. Thanks in advance.
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Random Loops
Let's see:
1. Do you want to choose numbers randomly between 1 and 20 for 3 seconds
and put them into a list?
2. What has this got to do with an initial field with numbers 1 to 20 in?
-
1. Do you want to choose numbers randomly between 1 and 20 for 3 seconds
and put them into a list?
2. What has this got to do with an initial field with numbers 1 to 20 in?
-
- Attachments
-
- 1to20.livecode.zip
- Here's the stack.
- (1.23 KiB) Downloaded 168 times
-
- Posts: 7
- Joined: Sat Jun 22, 2019 9:19 pm
Re: Random Loops
I guess I don't need the list in a field The posted example actually runs for 8 0r 9 seconds, and I was trying to use the seconds function, if that makes sense. Do I need to just estimate count and adjust the wait accordingly? It seems odd that I can't limit the time duration.
Re: Random Loops
Can you elaborate? Do you mean you want a display to update with randomly sorted values over and over and then stop after three seconds? (Thereby "picking" the top value which will be a random number from the pool?)
I have a vague idea along the lines of a lottery draw where the balls tumble around randomly until one is pulled out. Is that anything like what you are looking to do? If not, can you explain a bit more please?
I have a vague idea along the lines of a lottery draw where the balls tumble around randomly until one is pulled out. Is that anything like what you are looking to do? If not, can you explain a bit more please?
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Random Loops
mainly because I put wait 5 ticks between each random number so thatThe posted example actually runs for 8 0r 9 seconds
the whole thing didn't just go "whoosh" . . .
in the GO FOR IT button
Code: Select all
on mouseUp
put empty into of fld "BOOM"
put empty into of fld "NUMZ"
put 1 into KOUNT
repeat until Kount > 180
put random(20) into line KOUNT of fld "BOOM"
put KOUNT into fld "NUMZ"
add 1 to KOUNT
wait 5 ticks
end repeat
end mouseUp
any timing you need.
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Random Loops
And, unfortunately you cannot do this sort of thing:
pseudocode:
on mouseUp
put empty into of fld "BOOM"
put empty into of fld "NUMZ"
put 1 into KOUNT
repeat for 3 seconds
put random(20) into line KOUNT of fld "BOOM"
put KOUNT into fld "NUMZ"
add 1 to KOUNT
end repeat
end mouseUp
pseudocode:
on mouseUp
put empty into of fld "BOOM"
put empty into of fld "NUMZ"
put 1 into KOUNT
repeat for 3 seconds
put random(20) into line KOUNT of fld "BOOM"
put KOUNT into fld "NUMZ"
add 1 to KOUNT
end repeat
end mouseUp
Re: Random Loops
Sure, but you can
on mouseUp
set the cTimeUp of me to false
put empty into fld "BOOM"
put empty into fld "NUMZ"
put 1 into KOUNT
send "endTimer" to me in 3 seconds
repeat until the cTimeUp of me is true
put random(20) into line KOUNT of fld "BOOM"
put KOUNT into fld "NUMZ"
add 1 to KOUNT
end repeat
end mouseUp
on endTimer
set the cTimeUp of me to true
end endTimer
But I am not sure whether this is actually what the OP wants to address.
on mouseUp
set the cTimeUp of me to false
put empty into fld "BOOM"
put empty into fld "NUMZ"
put 1 into KOUNT
send "endTimer" to me in 3 seconds
repeat until the cTimeUp of me is true
put random(20) into line KOUNT of fld "BOOM"
put KOUNT into fld "NUMZ"
add 1 to KOUNT
end repeat
end mouseUp
on endTimer
set the cTimeUp of me to true
end endTimer
But I am not sure whether this is actually what the OP wants to address.
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Random Loops
I did not look closely at the earlier replies, but something like this?
Craig
Code: Select all
on mouseUp
repeat with y = 1 to 1000
put Y into line y of tList --build list of numbers (or use your own)
end repeat
put the seconds into tStart
repeat until the seconds = tStart + 3
get random(the number of lines of tList)
put line it of tList & return after accum
delete line it of tList
wait 10
end repeat
answer accum
end mouseUp
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Random Loops
Richmond.
You don't ever answer your accums?
I have always used "temp" and "accum" for ephemeral variables. Never much latched onto "foo".
Craig
You don't ever answer your accums?
I have always used "temp" and "accum" for ephemeral variables. Never much latched onto "foo".
Craig
-
- Posts: 7
- Joined: Sat Jun 22, 2019 9:19 pm
Re: Random Loops
I think this is what I need. I figured out that I couldn't repeat for x seconds. I think I am confused by the dictionary entries for the seconds function vs keyword. When you put the seconds into tStart, is that starting at 0 seconds, or the number of seconds since the start of the eon? Since the line includes the seconds, isn't that the function? My second question is why wait 10?dunbarx wrote: ↑Sun Jun 23, 2019 1:44 amI did not look closely at the earlier replies, but something like this?CraigCode: Select all
on mouseUp repeat with y = 1 to 1000 put Y into line y of tList --build list of numbers (or use your own) end repeat put the seconds into tStart repeat until the seconds = tStart + 3 get random(the number of lines of tList) put line it of tList & return after accum delete line it of tList wait 10 end repeat answer accum end mouseUp
Re: Random Loops
When you get the seconds, it is the time, now, in seconds since the start of the epoch (1970, Jan 1).
So "the seconds + 3" is three seconds later, still measured from epoch start, but obviously 3 seconds difference.
wait 10 would have been only to introduce a delay to the loop so you don't have a locked up loop racing to provide so many random numbers in the three seconds that it chokes. (Wait "with messages" is often a factor here.)
(PS the send in time version is (or can be) rather more flexibly robust in this matter.)
So "the seconds + 3" is three seconds later, still measured from epoch start, but obviously 3 seconds difference.
wait 10 would have been only to introduce a delay to the loop so you don't have a locked up loop racing to provide so many random numbers in the three seconds that it chokes. (Wait "with messages" is often a factor here.)
(PS the send in time version is (or can be) rather more flexibly robust in this matter.)
Re: Random Loops
Was what I was thinking also, separate handler, send (me) to me in 1 second, 3 runs. I thought an earlier reply already had that in it though?
OH!~ that was you, SparkOut~! DOH
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Random Loops
Many ways to do things in LC.
The "send in time" gadget is powerful indeed, essentially creating a "loop" that as a bonus releases control back to the machine in each iteration. This is something that any of the "repeat" constructions cannot do.
That is a lesson in itself. You need to become familiar with that methodology.
But here, as others have said, the "seconds" is the current "time", and its actual value is irrelevant. I thought is was the simplest way to do what you wanted. There are two finer "time stamps", if you need that sort of thing, the "ticks" (each 1/60 of a second) and the "milliseconds" (each 1/1000 of a second). Each works the same way, just with greater resolution.
"Sending in time" seemed excessive in this case, and I could see no benefit.
Craig
The "send in time" gadget is powerful indeed, essentially creating a "loop" that as a bonus releases control back to the machine in each iteration. This is something that any of the "repeat" constructions cannot do.
That is a lesson in itself. You need to become familiar with that methodology.
But here, as others have said, the "seconds" is the current "time", and its actual value is irrelevant. I thought is was the simplest way to do what you wanted. There are two finer "time stamps", if you need that sort of thing, the "ticks" (each 1/60 of a second) and the "milliseconds" (each 1/1000 of a second). Each works the same way, just with greater resolution.
"Sending in time" seemed excessive in this case, and I could see no benefit.
Craig
-
- Posts: 720
- Joined: Thu Sep 11, 2014 1:49 pm
- Location: The Netherlands
Re: Random Loops
Made a version in which one can set the time between 1 and 10 seconds in button 'Go for it 2'.
Just for fun ...
Just for fun ...