## 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: Klaus, FourthWorld, heatherlaine, kevinmiller

codes4kids
Posts: 4
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.

richmond62
Livecode Opensource Backer
Posts: 3357
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?
-
Attachments
1to20.livecode.zip
Here's the stack.

codes4kids
Posts: 4
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.

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

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

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

### Re: Random Loops

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"
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
Posts: 3357
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"
end repeat
end mouseUp

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

### 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"
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
Posts: 6074
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?

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
end mouseUp``````
Craig

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

### Re: Random Loops

?

Do tell.

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

### Re: Random Loops

Richmond.

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

Craig

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

### Re: Random Loops

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
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: 1983
Joined: Sun Sep 23, 2007 4:58 pm

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

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

### Re: Random Loops

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

dunbarx
VIP Livecode Opensource Backer
Posts: 6074
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

mrcoollion
Posts: 396
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 ...

1to20_MrCoolLion.zip