Think of it as a series of conversions of ranges. The RND function provides you with the range [0,1), and you want the range [40,51) (note the exclusion at the upper end). The first step should be to set the magnitude of the ranges. [40,51) is the same magnitude (size) as [0,11), so you need to multiply the value returned by RND by 11 (note that this value is the lower bound subtracted from the upper bound). After this point, you have the range [0,11), and you want to translate this up to [40,51). This is easy; just add 40.
The rule of converting ranges is that whatever you do your number you also do to both ends of the range.
That said, the following function will concisely return an integer inside the specified range, inclusively at both ends:
[tt] FUNCTION getRandomInteger%(leastNumber%, greatestNumber%)
rangeSize% = greatestNumber% - leastNumber% + 1
getRandomInteger% = (rangeSize% * RND) + leastNumber% END FUNCTION[/tt]
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.