Random Loops

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

codes4kids
Posts: 7
Joined: Sat Jun 22, 2019 9:19 pm

Random Loops

Post by codes4kids » Sat Jun 22, 2019 9:24 pm

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.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Random Loops

Post by richmond62 » Sat Jun 22, 2019 9:46 pm

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
Attachments
1to20.livecode.zip
Here's the stack.
(1.23 KiB) Downloaded 165 times

codes4kids
Posts: 7
Joined: Sat Jun 22, 2019 9:19 pm

Re: Random Loops

Post by codes4kids » Sat Jun 22, 2019 10:17 pm

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.

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: Random Loops

Post by SparkOut » Sat Jun 22, 2019 10:33 pm

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?

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Random Loops

Post by richmond62 » Sat Jun 22, 2019 10:42 pm

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.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Random Loops

Post by richmond62 » Sat Jun 22, 2019 10:56 pm

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

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: Random Loops

Post by SparkOut » Sat Jun 22, 2019 11:45 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Random Loops

Post by dunbarx » 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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9287
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Random Loops

Post by richmond62 » Sun Jun 23, 2019 8:07 am

"answer accum"

?

Do tell.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Random Loops

Post by dunbarx » Sun Jun 23, 2019 2:38 pm

Richmond.

You don't ever answer your accums?

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

Craig

codes4kids
Posts: 7
Joined: Sat Jun 22, 2019 9:19 pm

Re: Random Loops

Post by codes4kids » Sun Jun 23, 2019 3:22 pm

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?

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: Random Loops

Post by SparkOut » Sun Jun 23, 2019 6:59 pm

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.)

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Random Loops

Post by bogs » Sun Jun 23, 2019 7:16 pm

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
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Random Loops

Post by dunbarx » Mon Jun 24, 2019 3:34 am

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

mrcoollion
Posts: 719
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Random Loops

Post by mrcoollion » Mon Jun 24, 2019 7:25 am

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 179 times

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”