public static float Normalize(ref UMT.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; }
public static Vector2 Disk( ref UMT.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)) ); }
/// <summary> /// Returns a point on the unit sphere that is within the outer cone along the z-axis /// but not inside the inner cone. The resulting area describes a ring on the sphere surface. /// </summary> /// <param name="innerSpotAngle">[0..180] specifies the inner cone that should be excluded.</param> /// <param name="outerSpotAngle">[0..180] specifies the outer cone that should be included.</param> /// <remarks>FROM: http://unifycommunity.com/wiki/index.php?title=UnitSphere</remarks> public static Vector3 GetPointOnRing(float innerSpotAngle, float outerSpotAngle, ref UMT.MersenneTwister _rand) { float angle1 = MTRandom.ScaleFloatToRange(_rand.NextSingle(true),0.0f, Mathf.PI*2, 0, 1); float angle2 = MTRandom.ScaleFloatToRange(_rand.NextSingle(true),innerSpotAngle, outerSpotAngle, 0, 1) * Mathf.Deg2Rad; Vector3 V = new Vector3(Mathf.Sin(angle1),Mathf.Cos(angle1),0); V *= Mathf.Sin(angle2); V.z = Mathf.Cos(angle2); return V; }
public static Vector2 Circle( ref UMT.MersenneTwister _rand, MTRandom.Normalization n, float t ) { float r; switch (n) { case MTRandom.Normalization.STDNORMAL: r = MTRandom.ScaleFloatToRange( (float) NormalDistribution.Normalize(_rand.NextSingle(true), t), 0, Int32.MaxValue, 0, 1); break; case MTRandom.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 = MTRandom.ScaleFloatToRange(r, 0, _2pi, 0, Int32.MaxValue); return new Vector2( (float) Math.Cos(a) , (float) Math.Sin(a)); }
public static Vector3 Volume(ref UMT.MersenneTwister _rand, MTRandom.Normalization n, float t) { float x, y, z; x = y = z = 0; switch (n) { case MTRandom.Normalization.STDNORMAL: x = (float) NormalDistribution.Normalize(_rand.NextSingle(true), t); y = (float) NormalDistribution.Normalize(_rand.NextSingle(true), t); z = (float) NormalDistribution.Normalize(_rand.NextSingle(true), t); break; case MTRandom.Normalization.POWERLAW: x = (float) PowerLaw.Normalize(_rand.NextSingle(true), t, 0, 1); y = (float) PowerLaw.Normalize(_rand.NextSingle(true), t, 0, 1); z = (float) PowerLaw.Normalize(_rand.NextSingle(true), t, 0, 1); break; default: x = _rand.NextSingle(true); y = _rand.NextSingle(true); z = _rand.NextSingle(true); break; } // Move to -1, 1 space as for CIRCLE and SPHERE return new Vector3((2*x)-1, (2*y)-1, (2*z)-1); }
/// <summary> /// 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 /// </summary> /// <param name="_rand">random generator.</param> /// <param name="xm">Xm.</param> public static float Normalize( ref UMT.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 - 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-gammln(em+1.0f)-g); } while (_rand.NextSingle(true) > t); } return (float) em; }
public static Vector2 Disk( ref UMT.MersenneTwister _rand, MTRandom.Normalization n, float temp ) { double t, theta; switch (n) { case MTRandom.Normalization.STDNORMAL: t = NormalDistribution.Normalize(_rand.NextSingle(true), temp); theta = NormalDistribution.Normalize(_rand.NextSingle(true), temp) * 2 * Math.PI; break; case MTRandom.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 Vector3 Surface(ref UMT.MersenneTwister _rand, MTRandom.Normalization n, float t) { Vector3 pos = new Vector3(); switch (n) { case MTRandom.Normalization.STDNORMAL: pos = GetPointOnCubeSurface( (float) NormalDistribution.Normalize(_rand.NextSingle(true), t), (float) NormalDistribution.Normalize(_rand.NextSingle(true), t), _rand.Next(5)); break; case MTRandom.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(6)); 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 UMT.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)); }
private static Vector3 PickCubePoints( ref UMT.MersenneTwister _rand ) { float x = MTRandom.ScaleFloatToRange( _rand.NextSingle(true), -1, 1, 0, 1 ); float y = MTRandom.ScaleFloatToRange( _rand.NextSingle(true), -1, 1, 0, 1 ); float z = MTRandom.ScaleFloatToRange( _rand.NextSingle(true), -1, 1, 0, 1 ); return new Vector3(x,y,z); }
public static Vector3 Volume(ref UMT.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); }
public static Vector3 Surface(ref UMT.MersenneTwister _rand) { // Move to -1, 1 space as for CIRCLE and SPHERE Vector3 pos = GetPointOnCubeSurface(_rand.NextSingle(true),_rand.NextSingle(true),_rand.Next(6)); return new Vector3((2*pos.x)-1, (2*pos.y)-1, (2*pos.z)-1); }