randomizing images in a folder

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

sabiam3
Posts: 9
Joined: Fri Jun 20, 2014 6:46 pm

randomizing images in a folder

Post by sabiam3 » Wed Jul 09, 2014 5:37 pm

Hi guys,

What function can I write to randomize the order of images, without replacement? I'm displaying a series of images from a preselected folder in my app, and I'm unsure how to randomize the images. Most of the other things I've found online regarding this problem address selecting a random integer. Is there a way I can get LiveCode to select a random image from a folder? Or is the better option to randomize the order of images beforehand, then have the program run through them one at a time? Thanks for the help!

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

Re: randomizing images in a folder

Post by dunbarx » Wed Jul 09, 2014 5:49 pm

Hi.

Well, if you already have seen how to randomize integers without loss or duplication, why not name your images in such a way that you can pull them from your folder in that manner? You might have "img1", "img2". Then you can (pdeudocode):

repeat for the number of images in yourFolder
load image "img" & yourRandomFunction
set the loc of last image to yourFavoriteLoc
end repeat

Craig Newman

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: randomizing images in a folder

Post by [-hh] » Wed Jul 09, 2014 6:19 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 3:32 pm, edited 2 times in total.
shiftLock happens

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: randomizing images in a folder

Post by Mark » Wed Jul 09, 2014 8:45 pm

Hi,

Suppose that variable myDir contains the path to the folder that contains your picture files. Then select a file with

Code: Select all

set the defaultFolder to myDir
put the files into myFiles
filter myFiles without ".*"
put myDir & slash & any line of myFiles into myFilename
set the filename of img 1 to myFilename
The problem with this syntax might be that pictures tend to appear multiple times, but that's the consequence of a really random routine. If you don't want pictures to appear multiple times, you need slightly longer code:

Code: Select all

local lFiles
on setFilename
  if lFiles is empty then
    set the defaultFolder to myDir
    put the files into lFiles
    filter lFiles without ".*"
    sort lines of lFiles by random(the number of lines of lFiles)
  end if
  put line 1 of lFiles into myFilename
  delete line 1 of myFilename
  set the filename of img 1 to myFilename
end setFilename
Don't forget to change the reference to img 1.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

jiml
Posts: 339
Joined: Sat Dec 09, 2006 1:27 am

Re: randomizing images in a folder

Post by jiml » Thu Jul 10, 2014 6:00 pm

If you're not concerned about the same image appearing more than once, you could put the list of files into fileList then 'get any line of fileList' whenever you want to display an image.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: randomizing images in a folder

Post by Mark » Thu Jul 10, 2014 6:04 pm

JML, that's the first part of my answer.
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: randomizing images in a folder

Post by [-hh] » Thu Jul 10, 2014 6:59 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 3:32 pm, edited 1 time in total.
shiftLock happens

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: randomizing images in a folder

Post by Mark » Thu Jul 10, 2014 7:28 pm

hh, In my opinion you're making too big of a deal out of this. Let's wait and see what OP has to say about it.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: randomizing images in a folder

Post by [-hh] » Thu Jul 10, 2014 7:58 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 3:33 pm, edited 1 time in total.
shiftLock happens

sabiam3
Posts: 9
Joined: Fri Jun 20, 2014 6:46 pm

Re: randomizing images in a folder

Post by sabiam3 » Fri Jul 11, 2014 8:58 pm

Thanks for the help guys. I was really looking for a function that would allow for the pictures to never appear more than once. Thanks for everyone's input!

sabiam3
Posts: 9
Joined: Fri Jun 20, 2014 6:46 pm

Re: randomizing images in a folder

Post by sabiam3 » Fri Jul 11, 2014 9:01 pm

Oh, and as for the question in this thread regarding the number of permutations and such things: the number of images I'll be using for this particular application isn't large enough for the consideration of the distribution of possibilities to be a major concern.

sabiam3
Posts: 9
Joined: Fri Jun 20, 2014 6:46 pm

Re: randomizing images in a folder

Post by sabiam3 » Fri Jul 11, 2014 9:37 pm

Mark wrote: Don't forget to change the reference to img 1.
What does this mean exactly? What should I be changing it to?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: randomizing images in a folder

Post by Mark » Fri Jul 11, 2014 9:42 pm

Hi,

In my script, I wrote

Code: Select all

  set the filename of img 1 to myFilename
but this could also be any of the following or even something completely different:

Code: Select all

  set the filename of img 2 to myFilename
  set the filename of img id 23005 to myFilename
  set the filename of img "My Picture" to myFilename
  // etc.
Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

sabiam3
Posts: 9
Joined: Fri Jun 20, 2014 6:46 pm

Re: randomizing images in a folder

Post by sabiam3 » Fri Jul 11, 2014 10:04 pm

Mark wrote:Hi,

In my script, I wrote

Code: Select all

  set the filename of img 1 to myFilename
but this could also be any of the following or even something completely different:

Code: Select all

  set the filename of img 2 to myFilename
  set the filename of img id 23005 to myFilename
  set the filename of img "My Picture" to myFilename
  // etc.
Kind regards,

Mark
I'm still confused by this. Am I giving the image some sort of arbitrary name, or should I be calling it by its file name in my computer?

sabiam3
Posts: 9
Joined: Fri Jun 20, 2014 6:46 pm

Re: randomizing images in a folder

Post by sabiam3 » Fri Jul 11, 2014 10:07 pm

Also, I can't seem to get the image to show up on the card where I want it to go. I've been using an image object, but I have no clue if that's the right way to go or not considering it doesn't seem to be working. I've already put the folder that I want to use and its files into variables, and I've called those variables in a script for a random image file to be shown, but it just isn't cooperating.

Post Reply