I'm on my phone, so excuse me if there's any problems
function randomPointInCircle(%radius) {
%angle = getRandom(0, 2*$pi);
%magnitude = getRandom(0, %radius);
%x = %magnitude * mCos(%angle);
%y = %magnitude * mSin(%angle);
return %x SPC %y;
}
I'm fairly certain torque uses radians for mSin and mCos. If not just replace 2*$pi with 360.
Now, that will just get a random point in a circle. For a circular spread, we can take a random point in a circle, say, 1 unit forward, and then find a vector from the muzzle to the circular plane.
function getRandomVector(%velocity, %radius, %dist) {
%random = randomPointInCircle(%radius);
%xAngle = mAtan(getWord(%random, 0)/%dist);
%zAngle = mAtan(getWord(%random, 1)/%dist);
%x = %velocity * mSin(%xAngle);
%y = %velocity * mCos(%xAngle);
%z = %velocity * mSin(%zAngle);
return %x SPC %y SPC %z;
}
Now, this assumes your muzzle is facing north and is horizontal. I'm sure there's some sort of vector rotation function that you can use with both the random vector and the muzzle's rotation.
I'm fairly certain this should work but no guarantees. It should atleast point you in the right direction.