I'm aware of that. That is why I suggested using combination of current and optimized approach based on certain condition. This would allow people to know that optimization is optional.
But regardless you should notify people that would potentially use your code about possible caveats that such code represents.
I'm afraid your math won't hold. Why? Because it is quite possible that your random algorithm would generate same pair of X and Y coordinates multiple times. Now why is that? When generating random numbers most random number algorithms internally generate random floating value between 0 and 1 and then this number is linearly translated to integer value where internal value of 1 would be translated to maximum integer value you specified. This results in rounding the internal number at some time which means that several near floating numbers would translate to same output integer value and thus also the possibility for generating same pair of output values.
Easy solution.
If your snake size equals to the number of all tiles on the map "Congratulations you won the game"
Bookmarks