public static float Normalize(ref NPack.MersenneTwister _rand, int ia) { int j; float am, e, s, v1, v2, x, y; if (ia < 1) throw new ArgumentException("Error in Gamma Distribution. Argument ia should be an integer > 1"); if (ia < 6) { // use direct method, addin waiting times x = 1.0f; for (j=1; j<=ia; j++) x *= _rand.NextSingle(true); x = (float) - (Math.Log(x)); } else { do { do { // This four lines generate the tanget of random angle // Equivalent to y = tan( pi * rand()) do { v1 = _rand.NextSingle(true); v2 = 2.0f * _rand.NextSingle(true) - 1.0f; } while (v1 * v1+v2 * v2 > 1.0f); y = v2/v1; am = ia-1; s = (float) Math.Sqrt(2.0*am+1.0f); x = s * y + am; // We decide wheter to reject x, Reject in 0 probability region } while (x <= 0.0f); e = (float) ((1.0f + y*y) * Math.Exp(am * Math.Log(x/am) -s*y)); } while (_rand.NextSingle(true) > e); } return x; }
// CIRCLE with R=1 public static Vector2 Circle( ref NPack.MersenneTwister _rand ) { float t = (float) _rand.Next(); float _2pi = (float) Math.PI * 2; float a = SpecialFunctions.ScaleFloatToRange(t, 0, _2pi, 0, Int32.MaxValue); return new Vector2( (float) Math.Cos(a) , (float) Math.Sin(a)); }
public static Vector2 Disk( ref NPack.MersenneTwister _rand ) { // t [0,1] , Theta [0,2pi) double t = _rand.NextSingle(true); // in range [0,1) then multiply this number by k to get a random number in the range [0,k) double theta = _rand.NextSingle(false) * 2 * Math.PI; return new Vector2( (float) (Math.Sqrt(t) * Math.Cos(theta)), (float) (Math.Sqrt(t) * Math.Sin(theta)) ); }
/// FROM: http://unifycommunity.com/wiki/index.php?title=UnitSphere /// <summary> /// Returns a point on the unit sphere that is within a cone along the z-axis /// </summary> /// <param name="spotAngle">[0..180] specifies the angle of the cone. </param> public static Vector3 GetPointOnCap(float spotAngle, ref NPack.MersenneTwister _rand) { float angle1 = SpecialFunctions.ScaleFloatToRange(_rand.NextSingle(true),0.0f, Mathf.PI*2, 0, 1); float angle2 = SpecialFunctions.ScaleFloatToRange(_rand.NextSingle(true), 0.0f,spotAngle * Mathf.Deg2Rad, 0, 1); Vector3 V = new Vector3(Mathf.Sin(angle1),Mathf.Cos(angle1),0); V *= Mathf.Sin(angle2); V.z = Mathf.Cos(angle2); return V; }
public static Vector3 Volume(ref NPack.MersenneTwister _rand) { Vector3 pos = PickCubePoints(ref _rand); while ( isNotInsideSphere(pos) ) { pos = PickCubePoints(ref _rand); } return pos; }
// RADIUS = 1 // Marsaglia Method (1972) I use for Surface and Volume // FROM http://stackoverflow.com/questions/5531827/random-point-on-a-given-sphere // ALSO: http://mathworld.wolfram.com/SpherePointPicking.html // 1. Pick a random point inside the [-1,1]x[-1,1]x[-1,1] cube // 2. If x*x + y*y + z*z > 1 repeat from 1 // 3. Normalize dividing x, y and z by Math.sqrt(x*x + y*y + z*z) public static Vector3 Surface(ref NPack.MersenneTwister _rand) { Vector3 pos = PickCubePoints(ref _rand); while ( IsNotOnSurface(pos) ) { pos = PickCubePoints(ref _rand); } return Normalize(pos); }
private static Vector3 PickCubePoints( ref NPack.MersenneTwister _rand ) { float x = SpecialFunctions.ScaleFloatToRange( _rand.NextSingle(true), -1, 1, 0, 1 ); float y = SpecialFunctions.ScaleFloatToRange( _rand.NextSingle(true), -1, 1, 0, 1 ); float z = SpecialFunctions.ScaleFloatToRange( _rand.NextSingle(true), -1, 1, 0, 1 ); return new Vector3(x,y,z); }
// INIT private void init(int size, DiceType type, ref NPack.MersenneTwister _rand) { _result = new int[size]; _dice_type = type; _size = size; for (int i = 0; i < _size; i++) { // Cast enum to int to get the value _result[i] = _rand.Next( 1, (int) type); } }
public static Vector2 Circle( ref NPack.MersenneTwister _rand, UnityRandom.Normalization n, float t ) { float r; switch (n) { case UnityRandom.Normalization.STDNORMAL: r = SpecialFunctions.ScaleFloatToRange( (float) NormalDistribution.Normalize(_rand.NextSingle(true), t), 0, Int32.MaxValue, 0, 1); break; case UnityRandom.Normalization.POWERLAW: r = (float) PowerLaw.Normalize(_rand.NextSingle(true), t, 0, Int32.MaxValue); break; default: r = (float) _rand.Next(); break; } float _2pi = (float) Math.PI * 2; float a = SpecialFunctions.ScaleFloatToRange(r, 0, _2pi, 0, Int32.MaxValue); return new Vector2( (float) Math.Cos(a) , (float) Math.Sin(a)); }
// return as a floating point number an integer value that is a random deviate drawn // from a Possion Distribution of mean xm using randx as a source of uniform deviates public static float Normalize( ref NPack.MersenneTwister _rand, float xm) { // Davide Rambaldi: all moved to double precision double sq, alxm, g, oldm; // oldm is a flag for wheter xm has changed or not sincle last call sq = alxm = g = oldm = (-1.0); double em, t, y; if (xm < 12.0f) { // Use direct method if (xm != oldm) { oldm = xm; g = Math.Exp(-xm); // if x is new, compute the exponential } em = -1; t = 1.0f; // Instead of adding exponential deviates it is equivalent to multiply unifomr deviates // We never actually take the log, we compare the precomupted exponential do { ++em; t *= _rand.NextSingle(true); } while (t > g); } else { // Use REJECTION method // xm has changed? if ( xm != oldm) { oldm = xm; sq = Math.Sqrt(2.0 * xm); alxm = Math.Log(xm); // Gammln is the natural log of a gamma function g = xm * alxm - SpecialFunctions.gammln(xm + 1.0f); } do { do { // y is the deviate from a Lorentzian comparison function y = Math.Tan(Math.PI*_rand.NextSingle(true)); em=sq*y+xm; } while (em < 0.0); em = Math.Floor(em); t = 0.9 * (1.0+y*y) * Math.Exp(em*alxm-SpecialFunctions.gammln(em+1.0f)-g); } while (_rand.NextSingle(true) > t); } return (float) em; }
public static Vector2 Disk( ref NPack.MersenneTwister _rand, UnityRandom.Normalization n, float temp ) { double t, theta; switch (n) { case UnityRandom.Normalization.STDNORMAL: t = NormalDistribution.Normalize(_rand.NextSingle(true), temp); theta = NormalDistribution.Normalize(_rand.NextSingle(true), temp) * 2 * Math.PI; break; case UnityRandom.Normalization.POWERLAW: t = PowerLaw.Normalize(_rand.NextSingle(true), temp, 0, 1); theta = PowerLaw.Normalize(_rand.NextSingle(true), temp, 0, 1) * 2 * Math.PI; break; default: t = (float) _rand.NextSingle(true); theta = _rand.NextSingle(false) * 2 * Math.PI; break; } return new Vector2( (float) (Math.Sqrt(t) * Math.Cos(theta)), (float) (Math.Sqrt(t) * Math.Sin(theta)) ); }
public static Vector2 Area( ref NPack.MersenneTwister _rand, UnityRandom.Normalization n, float t ) { float x,y; x = y = 0; switch (n) { case UnityRandom.Normalization.STDNORMAL: x = (float) NormalDistribution.Normalize(_rand.NextSingle(true), t); y = (float) NormalDistribution.Normalize(_rand.NextSingle(true), t); break; case UnityRandom.Normalization.POWERLAW: x = (float) PowerLaw.Normalize(_rand.NextSingle(true), t, 0, 1); y = (float) PowerLaw.Normalize(_rand.NextSingle(true), t, 0, 1); break; default: x = _rand.NextSingle(true); y = _rand.NextSingle(true); break; } // Move to -1, 1 space as for CIRCLE and SPHERE return new Vector2((2*x - 1), (2*y - 1)); }
public static Vector3 Surface(ref NPack.MersenneTwister _rand, UnityRandom.Normalization n, float t) { Vector3 pos = new Vector3(); switch (n) { case UnityRandom.Normalization.STDNORMAL: pos = GetPointOnCubeSurface( (float) NormalDistribution.Normalize(_rand.NextSingle(true), t), (float) NormalDistribution.Normalize(_rand.NextSingle(true), t), _rand.Next(5)); break; case UnityRandom.Normalization.POWERLAW: pos = GetPointOnCubeSurface( (float) PowerLaw.Normalize(_rand.NextSingle(true), t, 0, 1), (float) PowerLaw.Normalize(_rand.NextSingle(true), t, 0, 1), _rand.Next(5)); break; default: pos = GetPointOnCubeSurface(_rand.NextSingle(true),_rand.NextSingle(true),_rand.Next(5)); break; } // Move to -1, 1 space as for CIRCLE and SPHERE return new Vector3((2*pos.x)-1, (2*pos.y)-1, (2*pos.z)-1); }
public static Vector2 Area( ref NPack.MersenneTwister _rand ) { // Move to -1, 1 space as for CIRCLE and SPHERE return new Vector2((2*_rand.NextSingle(true) - 1), (2*_rand.NextSingle(true) - 1)); }
// CONSTRUCTOR public DiceRoll(int size, DiceType type, ref NPack.MersenneTwister _rand) { if (size < 1) throw new ArgumentOutOfRangeException ("Number of dices shlud be > 0"); init(size,type,ref _rand); }
public static Vector3 Surface(ref NPack.MersenneTwister _rand) { // Move to -1, 1 space as for CIRCLE and SPHERE Vector3 pos = GetPointOnCubeSurface(_rand.NextSingle(true),_rand.NextSingle(true),_rand.Next(5)); return new Vector3((2*pos.x)-1, (2*pos.y)-1, (2*pos.z)-1); }
// FIXME: NOT YET IMPLEMENTED IN UNITYRANDOM public static Vector3 GetPointOnRing(float innerSpotAngle, float outerSpotAngle, ref NPack.MersenneTwister _rand, Quaternion orientation) { return orientation * GetPointOnRing(innerSpotAngle, outerSpotAngle, ref _rand); }
// FIXME: NOT YET IMPLEMENTED IN UNITYRANDOM public static Vector3 GetPointOnRing(float innerSpotAngle, float outerSpotAngle, ref NPack.MersenneTwister _rand, Transform relativeTo, float radius) { return relativeTo.TransformPoint( GetPointOnRing(innerSpotAngle, outerSpotAngle, ref _rand)*radius ); }
public static Vector3 Volume(ref NPack.MersenneTwister _rand) { Vector3 pos = new Vector3(_rand.NextSingle(true), _rand.NextSingle(true), _rand.NextSingle(true)); // Move to -1, 1 space as for CIRCLE and SPHERE return new Vector3((2*pos.x)-1, (2*pos.y)-1, (2*pos.z)-1); }
// FIXME: NOT YET IMPLEMENTED IN UNITYRANDOM public static Vector3 GetPointOnCap(float spotAngle, ref NPack.MersenneTwister _rand, Quaternion orientation) { return orientation * GetPointOnCap(spotAngle, ref _rand); }
// FIXME: NOT YET IMPLEMENTED IN UNITYRANDOM public static Vector3 GetPointOnCap(float spotAngle, ref NPack.MersenneTwister _rand, Transform relativeTo, float radius) { return relativeTo.TransformPoint( GetPointOnCap(spotAngle, ref _rand)*radius ); }