Answers for "Random insideUnitSphere but outside other UnitSphere?"
http://answers.unity.com/questions/1324115/random-insideunitsphere-but-outsideunitsphere.html
The latest answers for the question "Random insideUnitSphere but outside other UnitSphere?"Answer by WILEz1975
http://answers.unity.com/answers/1324210/view.html
**Solved**:
![alt text][1]
The result in editor at runtime:
![alt text][2]
Work fine!
[1]: /storage/temp/89700-screenshot-7.jpg
[2]: /storage/temp/89701-screenshot-8.jpgFri, 10 Mar 2017 20:48:58 GMTWILEz1975Answer by _dns_
http://answers.unity.com/answers/1324145/view.html
**EDIT**: this answer does not work, my mistake, thanks @Bunny83 for pointing it out!
> 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)
**EDIT2 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 :-)Fri, 10 Mar 2017 15:48:38 GMT_dns_