I guess the your first example is interpreted like this:
random number from 1 to (1000 as text)
As for the explanation I’m not 100% sure, but you’re calling the function random number with two parameters from and to. Each parameter is parsed as an expression. Hence it combines 1000 with as text.
I got the function syntax from ScriptDebugger (Window > Dictionary, select Scripting Additions, and search for random number):
FUNCTION SYNTAX
set theResult to random number number ¬
from number ¬
to number ¬
with seed integer
random number only returns an integer if its direct parameter’s an integer or if both the from and to parameters are integers. My guess is that although (1000 as text) is probably coerced back to an integer for the number value, it’s not an integer at the point where its class is tested.
Maybe you’re going to quick there. AppleScript’s number conversion is smart, and converts to integer when it can, and otherwise it converts to real:
set myNum to 123.4 as text
log myNum -->"123.4" (locale depending)
log class of myNum --> text
set myNum to myNum as number
log myNum --> 123.4
log class of myNum --> real