Need Random, Non-repeating Images script

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
jiqoyn
Posts: 5
Joined: Sat Dec 19, 2015 9:42 pm

Need Random, Non-repeating Images script

Post by jiqoyn » Sat Dec 19, 2015 10:14 pm

Hello everyone,

I need a script for randomly selecting a single image out of a certain number images that will be hidden on a card and display it. The script also needs to not repeat an image until all images have cycled through. This script will be on "opencard" so each time the card gets opened it'll generate a new image.

I'll have a button that will take us back to a previous card. It should also hide whatever image is displayed on the current card while it transitions back to the previous card.

Any help is appreciated.

I've gotten the script to display a random image with "any image" but it often displays the same image back to back.

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

Re: Need Random, Non-repeating Images script

Post by dunbarx » Sat Dec 19, 2015 10:55 pm

Hi

Many fun ways to do this. Here is one, and it requires you to play along. If, after you do this, you still need help, write back.

On a new card make a button and two moderately sized fields. In field 1, put several very short lines of data. In the button script

Code: Select all

on mouseUp
   put fld 1 into rawData
   put "" into fld 2
   repeat until rawData = ""
      get random(the number of lines of rawData)
      put line it of rawData & return after fld 2
      delete line it of rawdata
   end repeat
end mouseUp
Do you see how this works? Do you see how you can use image references instead of whatever data you put into field 1? Step through if you need to, and watch the variables and fields.

Craig Newman

jiqoyn
Posts: 5
Joined: Sat Dec 19, 2015 9:42 pm

Re: Need Random, Non-repeating Images script

Post by jiqoyn » Sun Dec 20, 2015 12:01 am

Ok, I will try this now. Thank you.

jiqoyn
Posts: 5
Joined: Sat Dec 19, 2015 9:42 pm

Re: Need Random, Non-repeating Images script

Post by jiqoyn » Fri Jan 01, 2016 10:33 pm

I've gotten the script to work with general text data. But when I attempted to use an image reference, I cant get the image to display. It only displays the text image path information. I believe I'm missing a piece of data to make the script display the referenced path as an image.

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

Re: Need Random, Non-repeating Images script

Post by Klaus » Fri Jan 01, 2016 11:23 pm

Hi jiqoyn,

please post your script, otherwise we will have to guess and that is ineffective :D


Best

Klaus

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

Re: Need Random, Non-repeating Images script

Post by dunbarx » Sat Jan 02, 2016 12:17 am

What Klaus said.

But how do you "display" an image now? All I did was give you a way to arrange data from a list. In other words, whatever one-liner that worked before ought to work as well with a single instance from the derived list.

Craig

jiqoyn
Posts: 5
Joined: Sat Dec 19, 2015 9:42 pm

Re: Need Random, Non-repeating Images script

Post by jiqoyn » Sun Jan 03, 2016 7:50 pm

Here is the code that I'm using, it works for just lines of text:

on mouseUp
if field 3 = ""
then put field 1 into field 3

put fld 3 into rawData
put "" into fld 2
get random (the number of lines of rawData)
put line it of rawData & return after field 2

delete line it of rawData
end mouseUp

I've tried to change it to "show" an image but it doesnt work. I've set up a card in the stack that has my images on it, and I've tried to (1) reference those images from card 1; (2) I've also tried to go to the card that has the images on it and change the code a bit to make them display randomly but without success.

It frustrates me a bit bc it seems so easy but its a bit difficult to execute.
Also, when running the script ( which it works when just displaying text entries in the field) it sometimes doesnt change whats in the field. For example:
I have 1 field that has this information - line1, line2,line3,line4 in a row going down. I have 3 fields. field 1 is hidden, the script takes whatever is in field 1 and puts it into field 3. Then the script is supposed to randomly ( without repeating one until all of the entries have been chosen) choose another line. But I'm finding that the displayed line entry stays there after pushing the button once or twice. And it goes back and forth from 1 to 2 a few times without ever randomly cycling through. I hope that makes sense. What I'd want is a script that 'without repeating one, cycles through the entire list, and once all have been cycled through, to then start back over.'

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

Re: Need Random, Non-repeating Images script

Post by dunbarx » Mon Jan 04, 2016 3:20 am

Hi.

Is this just a distilled example of your handler? There is no loop that does what you described, or that mimics the one I offered a while back. Repeated clicking will now and then get the same line from field 1 displayed in field 2. As it should, Step through and see.

Review my example handler from two weeks ago. Run the experiment I suggested. What you end up with is a randomly sorted list without repeats. From there you can do whatever you want, like display images perhaps referenced in each line of that data.

But there is still that other issue, the displaying of an image. How do you do this now by hand? Do you show one image, and I am assuming the others are hidden? Whatever, that is trivial. The lesson is in the selection of non-repeating data.

Craig

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Need Random, Non-repeating Images script

Post by MaxV » Mon Jan 04, 2016 11:46 am

Probably you are looking for this:

########CODE#######
On openCard
#change working directory to the directory of this software
set itemDel to "/"
set the defaultFolder to item 1 to -2 of (the effective fileName of this stack)
#get the list of images already used
set the imageUsed of me to URL "file:./imageused.txt"
createImage
end openCard

on createImage
put the number of images of me into nImages
put random(nImages) into temp
if temp is not among the items of the imageUsed of me then
create image
set the imagedata of the last image to the imagedata of image temp
put temp & comma after URL "file:./imageused.txt"
else
createImage
end if
end createImage
#####END OF CODE#####
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

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

Re: Need Random, Non-repeating Images script

Post by Klaus » Mon Jan 04, 2016 1:59 pm

Hi Max,

I am sure now you managed to completely irritate the thread-starter.


Best

Klaus

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

Re: Need Random, Non-repeating Images script

Post by dunbarx » Mon Jan 04, 2016 3:35 pm

Klaus:

LOL

MaxV:

This student needs to be handled more gently and slowly.

jiqoyn:

Please write back with your progress. Do you see what I was getting at? That once you have your new list, if each line in that list was a reference to an image, you can pluck each one out in order and display it, each time hiding all the others. That requires another short snippet of code, and that is what I want you to see and perhaps even write for yourself.

Craig

jiqoyn
Posts: 5
Joined: Sat Dec 19, 2015 9:42 pm

Re: Need Random, Non-repeating Images script

Post by jiqoyn » Mon Jan 04, 2016 4:54 pm

lol no! he didn't irritate me. I just saw his reply. Once I get back to my pc this afternoon I'll test the original instructions. I don't know why I omitted the repeat statement. And once I get that working, I'll just need to know how to show the referenced image. Thanks everyone for your help.

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

Re: Need Random, Non-repeating Images script

Post by Klaus » Mon Jan 04, 2016 5:04 pm

Hi jiqoyn,

okie dokie :D
jiqoyn wrote:...I'll just need to know how to show the referenced image.
Instead of using several images, just set up ONE image object and simply:
...
set the filename of image "the only image object" to variable_containing_the_new_path_to_image_file
...


Best

Klaus

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

Re: Need Random, Non-repeating Images script

Post by dunbarx » Mon Jan 04, 2016 6:11 pm

What Klaus said.

Another way is to place all the images on a card and hide them. Then once you have your randomized list:

imageReference7
imageReference2
imageReference5
...

You can(pseudo):

Code: Select all

repeat with y = 1 to the number of lines of yourRandomizedList
hideImages --hide all images, simpler than finding out which one is actually visible and hiding that one
show image (line y of yourRandomizedList)
wait 30 --just to see it run
end repeat
Of course, you may not want to do it this way at all. But if you see how this goes, you can make it the way you want it.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Need Random, Non-repeating Images script

Post by jacque » Mon Jan 04, 2016 11:18 pm

A summary of how I'd approach this, assuming you have 4 imported images on a card already:

make a randomized list of numbers from 1 to 4
hide all 4 images
peel off either the first or last number in the random list
show that image
delete the line from the randomized list

The reason I say "first or last number" is because it will work either way, but under the hood, using the last one in the list will be faster if the list is very long. In your case the list is very short so it doesn't matter, but I often go from the back end just out of habit.

Here's one way to do it:

Code: Select all

on showRandomImage
  if fld 3 = empty then
    put fld 1 into fld 3
    sort fld 3 by random(the number of lines in fld 3)
  end if
  repeat with x = 1 to 4 -- hide them all first
    hide img x of cd "display" -- use the real card name here
  end repeat
  show img (last line of fld 3) of cd "display" -- show the chosen one
  delete last line of fld 3
end showRandomImage
There are better ways to handle this than using fields, but for now this should work. Later you'll want to use variables instead of fields. They execute much faster and don't clutter up the card with a lot of unnecessary objects.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply