mt_rand() allows you to specify a a minumum, and a maximum value.. So if you want 3 digits returned, you could use:
$randomnum = mt_rand(100,999);
for 4 digits:
$randomnum = mt_rand(1000,9999);
ect, ect. You can use variables for the value to allow you to change the value via the script if you need to.