Page 1 of 2

randomizing images in a folder

Posted: Wed Jul 09, 2014 5:37 pm
by sabiam3
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!

Re: randomizing images in a folder

Posted: Wed Jul 09, 2014 5:49 pm
by dunbarx
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

Re: randomizing images in a folder

Posted: Wed Jul 09, 2014 6:19 pm
by [-hh]
..........

Re: randomizing images in a folder

Posted: Wed Jul 09, 2014 8:45 pm
by Mark
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

Re: randomizing images in a folder

Posted: Thu Jul 10, 2014 6:00 pm
by jiml
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.

Re: randomizing images in a folder

Posted: Thu Jul 10, 2014 6:04 pm
by Mark
JML, that's the first part of my answer.

Re: randomizing images in a folder

Posted: Thu Jul 10, 2014 6:59 pm
by [-hh]
..........

Re: randomizing images in a folder

Posted: Thu Jul 10, 2014 7:28 pm
by Mark
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

Re: randomizing images in a folder

Posted: Thu Jul 10, 2014 7:58 pm
by [-hh]
..........

Re: randomizing images in a folder

Posted: Fri Jul 11, 2014 8:58 pm
by sabiam3
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!

Re: randomizing images in a folder

Posted: Fri Jul 11, 2014 9:01 pm
by sabiam3
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.

Re: randomizing images in a folder

Posted: Fri Jul 11, 2014 9:37 pm
by sabiam3
Mark wrote: Don't forget to change the reference to img 1.
What does this mean exactly? What should I be changing it to?

Re: randomizing images in a folder

Posted: Fri Jul 11, 2014 9:42 pm
by Mark
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

Re: randomizing images in a folder

Posted: Fri Jul 11, 2014 10:04 pm
by sabiam3
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?

Re: randomizing images in a folder

Posted: Fri Jul 11, 2014 10:07 pm
by sabiam3
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.