4476 (1) [Avatar] Offline
#1
In chapter 3, implementing the Surprise Me button, a random number is generated for the index of the image shown. However, sometimes this random index is the same index as the previous random index. To me, this feels buggy as I can click the mouse a bunch of times and not see anything change. At first, I thought it was an issue with the click event firing until I debug logged it.

I'd like to be able to exclude the previous random index from being generated again consecutively but haven't been able to figure it out yet. One thought I had was to re-run the random number generation if it produced the same result as the previous (even though I don't even like that solution as it seems wasteful) I couldn't get that working hehe.

Any advice would be appreciated! Enjoying the book and language!
rtfeldman (60) [Avatar] Offline
#2
Here's an idea for a way to do that:

1. Generate a random number from 1 to N, where N is the number of photos.
2. Add that number to the currently selected index. Since it starts at 1, this will always give a different number.
3. If the number is N or higher, subtract N from it to "wrap around."

Should give you a different answer every time, with one randomly generated number!