Your logic seems fine. I've tried this:
<img src="http://i47.tinypic.com/eq5sv8.png" border="0" />
Every time you click a sprite the system adds one to the second variable.
Then it picks every second a random instance within the given constraints.
A touch outside resets the second variable of all instances.