Sorting buttons by random

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
PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Sorting buttons by random

Post by PoLyGLoT » Mon Nov 04, 2013 12:57 am

Hi all,

I have a group of buttons on a card. I want these buttons to be sorted randomly each time the card opens. Any easy way to do this?

Thanks,
PoLyGLoT

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting buttons by random

Post by dunbarx » Mon Nov 04, 2013 4:29 am

Hi.

Sort buttons.

What do you mean? Sort their positions, as if you put them in a cup and shook them out onto the card? Sort their names or creation number?

Please write back.

Craig Newman

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: Sorting buttons by random

Post by PoLyGLoT » Mon Nov 04, 2013 2:11 pm

Hi,

I mean - shuffle their positions. For instance, their layout on the screen to vary each time the card is opened. I will not create any new buttons or generate anything new - rather, the same buttons will just be rearranged each time the card is opened.

Thank you for any assistance.

PoLyGLoT

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

Re: Sorting buttons by random

Post by Klaus » Mon Nov 04, 2013 2:19 pm

HI Poly,

maybe somethig like this:

Code: Select all

on preopencard
  ## I shuffle button 1 to 5 in this example:
  repeat with i = 1 to 5

   ## Collect LOCs in a list:
   put the loc of btn i & CR after tLocList
  end repeat

  ## Delete trailing CR
  delete char -1 of tLocList

  ## Randomize list:
  sort lines of tLocList by random(100000)

  ## Now set the locs:
  repeat with i = 1 to 5
   set the loc of btn i to line i of tLocList tLocList
  end repeat
end preopencard
You get the picture :D


Best

Klaus

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: Sorting buttons by random

Post by PoLyGLoT » Mon Nov 04, 2013 3:05 pm

Hi Klaus,

Thanks for the tip. I think there is a problem though - it leaves them too scattered on the screen.

Is there any code to automatically align certain buttons on the left and right?

Thanks,
PoLyGLoT

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

Re: Sorting buttons by random

Post by Klaus » Mon Nov 04, 2013 3:16 pm

Hi Poly,

aha, so you do not mean to SHUFFLE the exitsing positions
but rather spread all your button randomly over the card?

That will require some more "sophisticated" scripting :D


Bst

Klaua

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting buttons by random

Post by dunbarx » Mon Nov 04, 2013 8:08 pm

Hi.

So you need to set the boundaries of the area in which the buttons will fall, and create random locs that fall within those boundaries, and also do two other things. Can you say what those two are? If you do, I will give you hints on how to execute.

Unless someone else just tells you. But this is something that would be a great learning exercise.

Craig

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: Sorting buttons by random

Post by PoLyGLoT » Mon Nov 04, 2013 9:53 pm

Hi all,

Thanks as always for the help. I believe I've found a better way to do it for my purposes. Instead of using radio buttons, I'm going to use regular buttons that can contain script and advance the screen. Then, I'm just going to have fields of text next to each button, and randomly sort what goes into those text fields each time the card opens. This will bypass the "loc" and "size" code requirements (hopefully).

Thanks!
PoLyGLoT

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting buttons by random

Post by dunbarx » Mon Nov 04, 2013 10:58 pm

There is an old saying that $100 worth of analysis is worth $1,000,000 worth of programming.

Glad you worked it out.

Craig

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: Sorting buttons by random

Post by PoLyGLoT » Mon Nov 04, 2013 11:11 pm

Thanks again Klaus and Craig. Great info as always.

Nice phrase, too. :mrgreen:

Post Reply