Skip to content
Browse files

added sampling from normal distribution.

  • Loading branch information...
1 parent 3682119 commit 467fc2374696bc09eddaf0123d9fb040070f239f @skywaLKer518 committed Jan 29, 2012
Showing with 12 additions and 3 deletions.
  1. +7 −0 src/Data.java
  2. +4 −2 src/Environment.java
  3. +1 −1 src/main.java
View
7 src/Data.java
@@ -13,6 +13,13 @@ public static double sampleStandardNormalUnivariate(){ // return a sample from s
return r1 * Math.sqrt(-2 * Math.log(Math.abs(r1)) / tmp);
}
+ public static double sampleNormalUnivariate(double mean, double var ){ // variance = var
+ double a = sampleStandardNormalUnivariate();
+ return Math.sqrt(var) * a + mean;
+ }
+
+
+
void newData(){
}
View
6 src/Environment.java
@@ -15,8 +15,10 @@ public static double sampleStandardNormalUnivariate(){ // return a sample from s
return r1 * Math.sqrt(-2 * Math.log(Math.abs(r1)) / tmp);
}
-
-
+ public static double sampleNormalUnivariate(double mean, double var ){ // variance = var
+ double a = sampleStandardNormalUnivariate();
+ return Math.sqrt(var) * a + mean;
+ }
View
2 src/main.java
@@ -15,7 +15,7 @@ public static void main(String args[]) throws IOException{
double sum = 0;
for (int i = 0; i < 20000000; i ++)
{
- sum += c.sampleStandardNormalUnivariate();
+ sum += c.sampleNormalUnivariate(2,1);
}
System.out.println("sum = "+ sum +" ave = " + sum * 1.0 / 20000000);
return;

0 comments on commit 467fc23

Please sign in to comment.
Something went wrong with that request. Please try again.