Gaussian Distribution in circle on x and y axis

Hey, so I have a weapon script which is exactly like CS:GO, except for the weapon spread. As you know if you’ve played said shooter, you know that your bullets become less accurate the longer you hold the fire button. My script can do this, but it does so in a square using normal distribution.

I want it to instead be in a circle and using gaussian distribution! I don’t know quite how gaussian distribution works yet, but Ive found a short script that does it, but I can’t figure out how to do it in a circle!

Here is the necessary parts of my weapon script:

	public static double NextGaussianDouble()
		double U, u, v, S;
			u = 2.0 * Random.value - 1.0;
			v = 2.0 * Random.value - 1.0;
			S = u * u + v * v;
		while (S >= 1.0);
		double fac = Mathf.Sqrt(-2.0f * Mathf.Log((float)S) /  (float)S);	//I just changed the doubles to floats. Is there any point in using doubles at all in this case?
		return u * fac;

That was the gaussian distribution, and here is the part that makes the direction:

w.CurSpread += 1.0f/w.MaxAmmo;
			w.Spread = w.SpreadSpeed.Evaluate(w.CurSpread)*w.MaxSpread;
			float spread = w.Spread * (float)NextGaussianDouble();
			Vector3 x = ((weapon[selected].right * w.PatternX.Evaluate(w.CurSpread) * w.Recoil.x) + (weapon[selected].right * spread));
			Vector3 y = ((weapon[selected].up * w.PatternY.Evaluate(w.CurSpread) * w.Recoil.y) + (weapon[selected].up * spread));
			Vector3 direction =  (x + y + weapon[selected].forward*100);

This version of the script nails the distribution, but it sprays it in a diagonal line! I want it to spray in a circle! Please help!

Without looking into the details of your code, I can see that you’re only calling NextGaussianDouble once. If this is the case, then you are only going to get a distribution in one dimension.

If you want a two dimensional distribution, you need to call NextGaussianDouble more than once. How you use these values is up to you, but generally you would have a “x” amount and a “y” amount.

If you use the same value for your “x” and “y” amounts, you will get a one dimensional distribution on the diagonal line y = x. I think this is what’s happening, since you only seem to be calling NextGaussianDouble once.