Difference between revisions of "GetRandomPercent"

Jump to navigation Jump to search
655 bytes removed ,  02:12, 15 July 2006
The short vs. floats behavior is covered in set not random functions
imported>Gamall
imported>JOG
(The short vs. floats behavior is covered in set not random functions)
<pre>set randVal to min + (GetRandomPercent * (max - min)) / 99</pre>
 
'''Good Examples:'''
short dice
set dice to 1 + 0.06 * GetRandompercent ; => 1 to 6 (5.94 is truncated to 5)
float rnd
set rnd to 5/99 * Getrandompercent ; => 0.00 to 5.00
 
 
----
 
'''Bad Example'''
short rnd
set rnd to 5/99 * Getrandompercent
 
You could think this one would provide you with an integer between 0 and 5, but it won't. It always yields 0. Why is that ? Seeing ''5/99'', the TESCS observes that 5 and 99 are both integers, and stores ''5/99'' as an integer. The closest integer to ''5/99'' being 0, ''rnd'' will always be set to 0.
 
To avoid this mistake, which can be quite annoying at times, the quickest way is to force the TESCS into storing ''5/99'' as a float, by typing it ''5.0 / 99.0'' for instance. So the following code will work as intended.
short rnd
set rnd to 5.0 / 99 * Getrandompercent
 
 
[[Category: Functions]]
Anonymous user

Navigation menu