Random insideUnitSphere but outside other UnitSphere?

I have a question, sorry for my bad English.
I know and use Random.insideUnitSphere. Now I would like to create a random point inside the sphere, but outside another smaller sphere (with the same center).

Like:

randomValue = Random.insideUnitSphere  * ((1.0-smallSphereRadius/bigSphereRadius) + (smallSphereRadius/bigSphereRadius)) * bigSphereRadius;

To simplify/Optimize if needed

TESTED - but still imperfect answer

So, if I recall, I was trying to find a solution without normalizing because it still costs some CPU.

Solution with normalizing (corresponding to OP’s solution) :

float delta = BigRadius - SmallRadius;
float length = SmallRadius + delta * Random.value;
Vector3 position = Random.insideUnitSphere.normalized * length;

Solution without normalizing (Notice it’s using onUnitSphere)

float delta = BigRadius - SmallRadius;
float length = delta * Random.value;
Vector3 pos = Random.onUnitSphere * (BigRadius - length);

I’ve tested both and they seem to work the same.

I’ve timed both methods with a release build on an i7: method without normalizing is around 2.4 time faster (repeating a million time the last 2 lines, results might differ with IL2CPP). I could not see the internal code Unity uses because it’s in C++

That being said, visually, it seems that both methods generate more points in the center than in the outer sphere (obvious when Comparing with a simple Vector3 pos = Random.insideUnitSphere * BigRadius It shows that the distribution is not good with both methods (that rely on the same principle because the normalization is equivalent as taking a point on the sphere). There must be more math involved to have a uniform distribution (though it seems correct to discard values from Random.insideUnitSphere * BigRadius that are inside SmallRadius, not CPU efficient but uniform, as far as I understand)

EDIT 2 - better solution

Ok, so I’ve recalled what I was trying to do first: I was trying to “compress” the random points inside the sphere = imagine a sphere with random points in it, then you slowly inflate the center until it reaches SmallRadius size, compressing all the points but not ejecting them out of the sphere. I think this conserves the uniform distribution, but I’m not mathematically sure it does. Visually, it works well and looks uniform, even with extreme values of radius (SmallRadius = 0 and SmallRadius = BigRadius - epsilon)

Vector3 posInSphere = Random.insideUnitSphere;
float length = posInSphere.magnitude;
float ratioRadius = SmallRadius/BigRadius;
Vector3 pos =  (((1.0f-ratioRadius)*length + ratioRadius) / length) * BigRadius * posInSphere;

It involves computing a vector’s magnitude so it’s close to the cost of a normalization but it’s better distributed anyway :slight_smile:

Solved

// paste this code here so people can copy it

The result in editor at runtime:

Work fine!

I know this is an old post but a simple way to do this is to not use random inside. Use random on radius and change the radius to be random:

Random.onUnitSphere * Random.Range(minDistance, maxDistance);