Simultaneously updating fields

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

Post Reply
glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Simultaneously updating fields

Post by glenn9 » Sun Jan 24, 2021 8:37 pm

Hi,

I'm wanting to put a random letter in five separate fields but I'm wanting the fields to be updated simultaneously rather one after another.

I've worked out the code for 'one field after another' but struggling to see how I can get all the fields to update simultaneously.

Not sure if this is possible but grateful for any hints?

Many thanks,

Glenn

Code: Select all

on mouseup
   put "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z" into tLetters
   set itemdelimiter to ","
   sort items of tLetters by random(the number of items of tLetters)
   repeat with a = 1 to 5
      repeat for each item x in tLetters
         sort items of tLetters by random(the number of items of tLetters)
         put x into field a
      end repeat
   end repeat
end mouseup

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Re: Simultaneously updating fields

Post by richmond62 » Sun Jan 24, 2021 9:11 pm

Probably your code will work so quickly that no one will be able to tell that those 5 fields did not update simultaneously,
so relax, sit back and feel groovy.

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

Re: Simultaneously updating fields

Post by SparkOut » Sun Jan 24, 2021 9:58 pm

But you can just add "lock screen" before the repeat loop and "unlock screen" after it, then the screen will do the redrawing necessary for all the fields together.
You can also leave out the specific "unlock" command in such a case - the unlock will automatically happen at the end of the handler, whether explicitly instructed or not. If your processing was not in the "mouseUp" handler like this example, and you wanted to continue the code with some other processing but wanted the visual cue for the user that the field content was new then an explicit "unlock" might be needed.

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Simultaneously updating fields

Post by Klaus » Sun Jan 24, 2021 10:28 pm

Here some hints on how to speed up things, escpecially if more than five fields are involved!

Code: Select all

on mouseup
   put "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z" into tLetters
   
   ## Only count ONCE, saves time in the repeat loop. :-)
   put the num of items of tLetters into tNumOfLetters
   
   ## See above, this is the default ITEMDELIMITER, so no need to set it explicitely.
   ## set itemdelimiter to ","
   sort items of tLetters by random(tNumOfLetters)
   
   ## This(lock screen) will always speed up things when updating controls on a card!!
   lock screen
   repeat with a = 1 to 5
      repeat for each item x in tLetters
         sort items of tLetters by random(tNumOfLetters)
         put x into field a
      end repeat
   end repeat
   unlock screen
end mouseup
Not yet sure what the "repeat for each item x in tLetters" is doing exactly?
Here you overwrite the content of fld a -> tNumOfLetters = 26 times in the loop!?

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

Re: Simultaneously updating fields

Post by bogs » Sun Jan 24, 2021 10:45 pm

To add to what Richmond and SparkOut said and, more to your original question,
glenn9 wrote:
Sun Jan 24, 2021 8:37 pm
I've worked out the code for 'one field after another' but struggling to see how I can get all the fields to update simultaneously.

Not sure if this is possible but grateful for any hints?
...to update 5 fields at the same time would require the ability to use multi-threading, which Lc does not have (nor, I might add, do many other languages this high level).

That is interesting Klaus, but why would you not just....

Code: Select all

# I replaced all the commas with "" in tVar, but if you were starting from scratch...

on mouseUp
	put "abcdefghijklmnopqrstuvwxyz" into tVar

	repeat with a = 1 to 5
		put any character of tVar into field a
	end repeat
end mouseUp
aPic_anyLetter.png
...button button who has the button...
aPic_anyLetter.png (23.49 KiB) Viewed 6926 times
Took about 2 milliseconds to run 1 time.
:wink:
Last edited by bogs on Sun Jan 24, 2021 10:51 pm, edited 2 times in total.
Image

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

Re: Simultaneously updating fields

Post by SparkOut » Sun Jan 24, 2021 10:46 pm

Urgh, yes - actually reading the code is a bit baffling, I didn't try it, just referred to using "lock screen".

@Glenn are you just trying to put a random letter in each field? (If so, must it be unique?)

If uniqueness is not required then no need for all the nested loops and randomising at all:

Code: Select all

on mouseUp
   put "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z" into tLetters
   lock screen
   repeat with a = 1 to 5
      put any item of tLetters into field a
   end repeat
   unlock screen
end mouseUp
If unique letters in each field is required:

Code: Select all

on mouseup
   put "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z" into tLetters
   
   ## Only count ONCE, saves time in the repeat loop. :-)
   put the num of items of tLetters into tNumOfLetters

   sort items of tLetters by random(tNumOfLetters)
   
   ## This(lock screen) will always speed up things when updating controls on a card!!
   lock screen
   repeat with a = 1 to 5
      --tLetters is already random so we can just sequentially take the first value, then the next, etc
      put item a of tLetters into field a
   end repeat
   unlock screen
end mouseup

[edit] posted as bogs beat me in the typing, but also drawing similar conclusions :)

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

Re: Simultaneously updating fields

Post by bogs » Sun Jan 24, 2021 10:52 pm

Well, you *did* go the extra mile and present it both ways :mrgreen:

Just to add to what you said, though, if I were looking for truly unique results each time, I'd use random seed first :oops:
Image

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

Re: Simultaneously updating fields

Post by SparkOut » Sun Jan 24, 2021 10:59 pm

Nah, let the engine pick its own seed. That's random (or as pseudo-random as it gets).
Setting a seed just means you can replay the same "random" order again.
I'm not sure that's ever useful in normal circumstances. I think Richard mentioned a use-case to work back through cryptopgraphic processes.

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

Re: Simultaneously updating fields

Post by bogs » Sun Jan 24, 2021 11:09 pm

I think you might have the two of those backwards, at least, they would be in every other language :D

The random function in almost every other language is at best a 'psuedo' random, i.e. you will get the same initial result every time per run.

Random Seed uses the clock in most other languages, and you can actually set it to the milliseconds in this language. Here is what Max's wiki has about random seed...
https://livecode.fandom.com/wiki/RandomSeed wrote: Use the randomSeed property to ensure greater randomness when generating random numbers.

Changing the randomSeed property changes the pseudorandom numbers generated by the random function and used by the any keyword. Using the same seed creates the same sequence of pseudorandom values.

For example, if you call the random function five times to generate a list of five numbers, then change the randomSeed to another value(function), the next five calls to the random function will produce a different list of five numbers. However, if you set the randomSeed back to its original value(function) and call(command) the random function five more times, the list of five numbers is the same as the first list you generated.

LiveCode uses a new randomSeed every time the application is started up.
Image

Davidv
Posts: 77
Joined: Sun Apr 09, 2006 1:51 am

Re: Simultaneously updating fields

Post by Davidv » Sun Jan 24, 2021 11:23 pm

While we are having fun, this extends bogs' simple example to give unique letters while re-randomising each time. I am not sure that shows any benefit over picking off the ith letter of an already randomised list.

Code: Select all

on mouseUp
   lock screen
   put "abcdefghijklmnopqrstuvwxyz" into tAlpha
   repeat with i = 1 to 5
      get any char of tAlpha
      replace it with empty in tAlpha
      put it into field i
   end repeat
   unlock screen
end mouseUp
Regarding seeding or randomness in general, when I truly want randomness, for example in my password generator, I use randomBytes().

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

Re: Simultaneously updating fields

Post by bogs » Sun Jan 24, 2021 11:42 pm

Nice, I like how you remove the letter chosen from the pool so your sure it won't show up somewhere else Image
Image

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

Re: Simultaneously updating fields

Post by SparkOut » Mon Jan 25, 2021 12:02 am

Re: randomSeed

Every time use use a random function, the value will be randomised according to the engine's algorithms. The engine loads its own randomSeed value each time it starts up.
You do NOT get the same initial result each time (unless you load the same seed value). You generally don't need to change the seed, if you do, you are introducing your own constraints on the results, and making them artificially less random.
If you do change the seed in the expectation of creating more randomness, then the only way you could do that is by using a randomised function to produce a random seed value.

If you use the same seed value, then you will always get the same results. Let the engine pick its own seed, and you'll get as random a result as any other.

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

Re: Simultaneously updating fields

Post by bogs » Mon Jan 25, 2021 4:00 am

SparkOut wrote:
Mon Jan 25, 2021 12:02 am
Every time use use a random function, the value will be randomised according to the engine's algorithms. The engine loads its own randomSeed value each time it starts up.
You do NOT get the same initial result each time (unless you load the same seed value).
I've actually tested this out a year ago, so I know the result, but what you write is correct, i.e.

The engine loads its own randomSeed value each time it starts up.


If you test a run after loading the IDE, you will find that run will repeat each time you run it because the value it generates is still the same value.

If you then shut down the IDE and restart it, you will then get a different value (because the IDE has now generated a new 'seed').

On the other hand, if you tie randomSeed to say, the milliseconds, you will get far different randomness every time you run it (as long as some few milliseconds have passed so you get a new milliseconds), regardless of whether or not the IDE has been restarted. That is because the milliseconds are constantly changing, so the randomSeed value is constantly changing, if you see what I mean.

So, if you change the seed every run, you are not introducing less randomness, you are making it quite a lot more random.

Now, if you don't believe me, that is fine, you certainly have the right and I don't really mind :D

Here is a scooby snack for you...the long milliseconds!
Image
Image

Davidv
Posts: 77
Joined: Sun Apr 09, 2006 1:51 am

Re: Simultaneously updating fields

Post by Davidv » Mon Jan 25, 2021 5:20 am

Or, for a nicely random seed

Code: Select all

get randomBytes(2)
put byteToNum(byte 1 of it) & byteToNum(byte 2 of it) into aSeed
Add bytes and truncate lengths as desired.

Post Reply