Page 1 of 2

Random Loops

Posted: Sat Jun 22, 2019 9:24 pm
by codes4kids
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.

Re: Random Loops

Posted: Sat Jun 22, 2019 9:46 pm
by richmond62
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?
-
1to20.png

Re: Random Loops

Posted: Sat Jun 22, 2019 10:17 pm
by codes4kids
I guess I don't need the list in a field :D 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

Posted: Sat Jun 22, 2019 10:33 pm
by SparkOut
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?

Re: Random Loops

Posted: Sat Jun 22, 2019 10:42 pm
by richmond62
The posted example actually runs for 8 0r 9 seconds
mainly because I put wait 5 ticks between each random number so that
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
All you need to do is open the scriptEditor of the button and fool around with things to get
any timing you need.

Re: Random Loops

Posted: Sat Jun 22, 2019 10:56 pm
by richmond62
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

Re: Random Loops

Posted: Sat Jun 22, 2019 11:45 pm
by SparkOut
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.

Re: Random Loops

Posted: Sun Jun 23, 2019 1:44 am
by dunbarx
I did not look closely at the earlier replies, but something like this?

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
Craig

Re: Random Loops

Posted: Sun Jun 23, 2019 8:07 am
by richmond62
"answer accum"

?

Do tell.

Re: Random Loops

Posted: Sun Jun 23, 2019 2:38 pm
by dunbarx
Richmond.

You don't ever answer your accums?

I have always used "temp" and "accum" for ephemeral variables. Never much latched onto "foo".

Craig

Re: Random Loops

Posted: Sun Jun 23, 2019 3:22 pm
by codes4kids
dunbarx wrote:
Sun Jun 23, 2019 1:44 am
I did not look closely at the earlier replies, but something like this?

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
Craig
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?

Re: Random Loops

Posted: Sun Jun 23, 2019 6:59 pm
by SparkOut
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.)

Re: Random Loops

Posted: Sun Jun 23, 2019 7:16 pm
by bogs
SparkOut wrote:
Sun Jun 23, 2019 6:59 pm
(PS the send in time version is (or can be) rather more flexibly robust in this matter.)
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 :D

Re: Random Loops

Posted: Mon Jun 24, 2019 3:34 am
by dunbarx
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

Re: Random Loops

Posted: Mon Jun 24, 2019 7:25 am
by mrcoollion
Made a version in which one can set the time between 1 and 10 seconds in button 'Go for it 2'.
Just for fun ... :mrgreen:

1to20_MrCoolLion.zip
(1.17 KiB) Downloaded 270 times