08-21-2013, 02:45 PM
Just to complement my previous answer... My suggestion for INTRND(n) was that the compiler would generate an Assembly code equivalent to this:
The reason I suggest this behavior to be generated internally by the compiler, instead of provided as a library, is as follows: If n is a constant, then the compiler will know the number of bits and replace it directly above. Otherwise, if n is not constant, then it should generate a simple routine to find out this value.
Code:
'Generate random number between 0 and n-1
function INTRND(n AS UINTEGER) AS UINTEGER
DIM result AS UINTEGER
DIM bits AS UBYTE
LET bits = number of bits in value n-1
DO
LET result = RND MOD (1<<(bits-1))
LOOP UNTIL (result <= n-1)
RETURN result
end function
The reason I suggest this behavior to be generated internally by the compiler, instead of provided as a library, is as follows: If n is a constant, then the compiler will know the number of bits and replace it directly above. Otherwise, if n is not constant, then it should generate a simple routine to find out this value.