Montana Matt wrote:...
rgg wrote:Now, consider all the rows in this result set numbered 1 through U. The big difference is that every row number represents a valid image this time, so after using the same random sampling formula as before to determine R (but using this lower value U!) there is no need for looping anymore!
That's a much cleaner and much simpler solution. That one may even be so low profile that it could be run every time the front page is loaded.
Glad to hear it. I wasn't sure about it, because while the looping solution only has to retrieve one image per loop iteration, the select statement in this alternative has to scan the whole object table and, depending on the threshold, might have to deliver up to several tens of thousands of images. Not knowing the architecture of the SummitPost site, I couldn't judge how long that might take.
If you implement this, I just thought of a minor but tricky detail. The formula to generate
R is such that, mathematically, it will always be in the range [1,
U]. However, due to possible rounding errors, I can't rule out that in very rare cases it produces 0 or
U+1, which might cause the program to crash if you use it as an index for the result set. I realized this when I did some testing on the formula in Excel and got
U+1.
A safe (and fast) solution would be to change any
R smaller than 1 back to 1, and every
R larger than
U back to
U. Unless I made an error in the formula, or there is an error in the implementation later on, this event is so rare that it won't have a noticeable effect on the statistics.