randomizing images in a folder
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
randomizing images in a folder
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!
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
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
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
..........
Last edited by [-hh] on Wed Aug 13, 2014 3:32 pm, edited 2 times in total.
shiftLock happens
Re: randomizing images in a folder
Hi,
Suppose that variable myDir contains the path to the folder that contains your picture files. Then select a file with
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:
Don't forget to change the reference to img 1.
Kind regards,
Mark
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
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
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
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Re: randomizing images in a folder
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
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
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Re: randomizing images in a folder
..........
Last edited by [-hh] on Wed Aug 13, 2014 3:32 pm, edited 1 time in total.
shiftLock happens
Re: randomizing images in a folder
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
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
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Re: randomizing images in a folder
..........
Last edited by [-hh] on Wed Aug 13, 2014 3:33 pm, edited 1 time in total.
shiftLock happens
Re: randomizing images in a folder
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
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
What does this mean exactly? What should I be changing it to?Mark wrote: Don't forget to change the reference to img 1.
Re: randomizing images in a folder
Hi,
In my script, I wrote
but this could also be any of the following or even something completely different:
Kind regards,
Mark
In my script, I wrote
Code: Select all
set the filename of img 1 to myFilename
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.
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
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Re: randomizing images in a folder
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?Mark wrote:Hi,
In my script, I wrote
but this could also be any of the following or even something completely different:Code: Select all
set the filename of img 1 to myFilename
Kind regards,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.
Mark
Re: randomizing images in a folder
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.