Random annoyance

Scott Rossi scott at tactilemedia.com
Fri May 9 16:06:01 EDT 2003


Recently, "Shari"  wrote:

> I've just discovered an annoyance in the random() function.  (And
> according to the docs, it is not a bug.)
> 
> random(0) produces the result of "1"
> 
> One would assume that random(0) would always produce 0.
> 
> 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.
> 
> It is quicker to let the result be 0, which affects nothing, than to
> say "if the variable > 0 then... run the function"
> 
> The following failed when item 17 of terrData was 0:
> 
> add round((random(item 17 of terrData)) * multiplier) to \
>              item 56 of line x of gStats
> 
> Had to go thru and add a whole slew of "if..." statements to the code.
> 
> Even though the docs claim random(x) will always produce a number
> between 1 and x (and that x must be a positive integer), I tried
> random(-10) and got (-3) which was cool.


If you don't want to use a "slew" of if/then statements, you could simply
replace the random function with a single custom random function that checks
the terrData value:


add round((myRandom(item 17 of terrData)) * multiplier) to \
   item 56 of line x of gStats

function myRandom N
    if N = 0 then
        return 0
    else return random(N)
end myRandom


Regards,

Scott Rossi
Creative Director
Tactile Media, Multimedia & Design
-----
E: scott at tactilemedia.com
W: http://www.tactilemedia.com




More information about the metacard mailing list