Browse Source

added gaussian distributed random numbers

git-svn-id: https://svn.inf.ethz.ch/svn/lecturers/a2/trunk@7125 8c9fc860-2736-0410-a75d-ab315db34111
skoster 8 years ago
parent
commit
6f7bfc75e5
1 changed files with 15 additions and 0 deletions
  1. 15 0
      source/Random.Mod

+ 15 - 0
source/Random.Mod

@@ -95,6 +95,21 @@ TYPE
 		BEGIN
 			RETURN -Math.ln(Uniform())/mu
 		END Exp;
+		
+		PROCEDURE Gaussian*(): REAL; (*generates a normal distribution with mean 0, variance 1 using the Box-Muller Transform*)
+		VAR
+			x1,x2,w,y1,y2: REAL;
+		BEGIN
+			REPEAT
+				x1:=2.0*Uniform()-1;
+				x2:=2.0*Uniform()-1;
+				w:=x1*x1+x2*x2;
+			UNTIL w<1;
+			w:=Math.sqrt( (-2.0* Math.ln(w) ) /w);
+			y1:=x1*w;
+			(*y2:=x2*w*)
+			RETURN y1;
+		END Gaussian;
 
 	END Generator;