Anomalies
Dar Scott
dsc at swcp.com
Fri Jul 11 11:11:00 EDT 2003
On Friday, July 11, 2003, at 08:09 AM, Shari wrote:
>> Shari wrote:
>>
>>> I wonder if I should bombard Kevin with the anomalies I've had to
>>> find workarounds for... such as random(0) = 1
>>
>> Under what circumstances would random(0) be expected to provide a
>> useful
>> result? Why not save the clock cycles by using 0 directly?
>
> From my original post when discovering this anomaly:
>
> Why would one want to get random(0), you ask?
>
> If 0 is a variable, which could be any number, and you write a
> mathematical function, assuming that if the variable is 0, the end
> result of the function will also be zero, the function fails.
You are quite right about using 0 directly. You might have a variable
or other expression that needs to work in general. And you are right
that functions should be generalized as they naturally might.
However, I disagree that this is an anomaly.
The random(x) function is defined to return an integer in the range
from 1 up to round(x). We can think of the rounding as incidental and
think of only whole numbers. In that case it takes whole numbers 1 and
up. That excludes 0 from the domain of the function. The function is
not defined for 0. The function is not an anomaly, because 1 up to 0
is not meaningful. I think the function should not crash for numbers
outside the domain, but I hold it to nothing else. Well, I would be
upset if it returned a very long string.
You can define your own function, say, rnd(), based on random() and use
that in your scripts. It will take less time than sending a note to
Kevin. You can use that everywhere. It would be at most seven lines
long. If you decide you want random(x+1)-1 instead of the version with
the _if_, then you can change it in only one place. (And then the
function is only three lines long.)
Many built-in functions do force parameters to the closest valid value.
If that was done, you still would not get zero.
I, too, am not happy with some built-in functions. For example, I wish
mod worked differently, but I can make my own.
I would find useful a completely different random(), perhaps with
another name, such as rand(). One that takes a number between 0 and 1
inclusive and returns true or false. In this case, applying it to 0
would always return false. Perhaps this is the kind of thing you are
looking for. That can be built. It only has to be built once whether
scripted or built-in.
It is my opinion that this is one of those cases in which you bend to
the product and not the other way around.
Dar Scott
More information about the metacard
mailing list