Question: Is the min/max check really needed in the function?
FUNCTION p5random! (mn!, mx!)
IF mn! > mx! THEN
SWAP mn!, mx!
END IF
p5random! = RND * (mx! - mn!) + mn!
END FUNCTION
Let’s say I send it the values 6,1 to generate a number from 1 to 6. Let’s pseudo-solve it, without the swap.
p5random! = RND * (mx! - mn!) + mn!
P5 = RND * (1 - 6) + 6
P5 = RND * (-5) + 6
A random number will be generated from 0 to -4.9999999 (RND * -5). Add 6 to that and we get a value between 6 and 1.0000001.
Swapping the values give a return range between 1 and 5.9999999, which is subtly different, but is that difference worth the conditional check inside a function which gets called multiple times in a loop? Why bother with the speed bump in the program — especially since it just reduces possible functionality, in case the user might like to get results between 6 and 1, instead of between 1 and 6.
When it comes to this type of error-checking, I’d rather ignore it, and trust it won’t make much difference even inside a program where the values accidentally get swapped. No need to add automatic checks which I can manually code for, when truly necessary, myself.
*********************
With that said, I doubt changing it would make much actual difference in execution speed here. The values are hard-coded and the min is always less than the max, so that SWAP never actually takes place for us. A single IF check takes very little time to execute, and without any swapping going on (Which can slow down performance by shuffling memory values.), the change in speed is going to be rather small...
(But, as Rho pointed out with the error-checking with arrays and $CHECKING:OFF, that minute delay can add considerable lag to performance, if called multiple times in large, inner loops.)
I’m just curious if it’s really necessary, or if it might be something you’d want to remove from the p5random function, since the variance in the results is so small, and since the check actually swaps values so rarely. (In this program, it doesn’t swap them at all.)