It should work this way. Maybe it could be done more effectively, dunno.
Anyway:
[source]
// lets presume D3DXVECTOR3 rayDirection is already defined and contains your ideal shooting direction vector
// first we need to find any vector perpendicular to rayDirection. There is infinite number of such vectors, we need any one of them. This could be done for example this way:
D3DXVec3 perpVec(rayDirection.y, -rayDirection.x, 0);
// when using this very simple way you would however have to make sure that you don't get 0 vector by this, which would happen if both rayDirection.x and rayDirection.y were zero. In this case you would have to use slightly different core, for example:
//D3DXVec3 perpVec(0, rayDirection.z, -rayDirection.y);
// The whole point of finding a perpendicular vector this way is to swap two components, change the sign of ONE of them and make the 3rd component zero.
// Normalize it for safety:
D3DXVec3Normalize(&perpVec, &perpVec);
// Now the shooting accuraty:
float shootingDeviationAngle = GetRandomNumberInInterval(0, maxDeviationAngleForCurrentWeapon); // this is partly pseudo-code, but I think it's clear what it does. Values are in radians!
// Rotate vector rayDirection around vector perpVec by the shootingDeviationAngle angle:
Matrix m;
Vec3 v;
D3DXMatrixRotationAxis(&m, &perpVec, shootingDeviationAngle);
D3DXVec3TransformNormal(&v, &rayDirection, &m);
// Get a random angle from 0 to 360 degrees:
float angle = GetRandomNumberInInterval(0, D3DX_PI * 2.0f);
// And rotate vector v around vector rayDirection by this angle:
D3DXMatrixRotationAxis(&m, &rayDirection, angle);
Vec3 finalRayDirection;
D3DXVec3TransformNormal(&finalRayDirection, &v, &m);
[/source]