Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
1 Answer
Steven Parker
243,656 PointsThere are a couple of approaches that would work here.
One would be to check each random pick against the list of previously chosen items, and if found, discard it and pick another. Since you're picking less than half of the total set this should be reasonably efficient.
Another way would be to start with a list of available choices, and as each random pick is made, remove the chosen item from the list. Then the next choice would be made from the reduced set. This method works well even when picking every item of the set.
Cheo R
37,150 PointsCheo R
37,150 PointsYou could have a variable that keeps track of which images are currently being displayed.
If an image is currently displayed, skip to the next available one.