Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added sampling from normal distribution.

  • Loading branch information...
commit 467fc2374696bc09eddaf0123d9fb040070f239f 1 parent 3682119
Kuan Liu authored

Showing 3 changed files with 12 additions and 3 deletions. Show diff stats Hide diff stats

  1. +7 0 src/Data.java
  2. +4 2 src/Environment.java
  3. +1 1  src/main.java
7 src/Data.java
@@ -13,6 +13,13 @@ public static double sampleStandardNormalUnivariate(){ // return a sample from s
13 13 return r1 * Math.sqrt(-2 * Math.log(Math.abs(r1)) / tmp);
14 14 }
15 15
  16 + public static double sampleNormalUnivariate(double mean, double var ){ // variance = var
  17 + double a = sampleStandardNormalUnivariate();
  18 + return Math.sqrt(var) * a + mean;
  19 + }
  20 +
  21 +
  22 +
16 23 void newData(){
17 24 }
18 25
6 src/Environment.java
@@ -15,8 +15,10 @@ public static double sampleStandardNormalUnivariate(){ // return a sample from s
15 15 return r1 * Math.sqrt(-2 * Math.log(Math.abs(r1)) / tmp);
16 16 }
17 17
18   -
19   -
  18 + public static double sampleNormalUnivariate(double mean, double var ){ // variance = var
  19 + double a = sampleStandardNormalUnivariate();
  20 + return Math.sqrt(var) * a + mean;
  21 + }
20 22
21 23
22 24
2  src/main.java
@@ -15,7 +15,7 @@ public static void main(String args[]) throws IOException{
15 15 double sum = 0;
16 16 for (int i = 0; i < 20000000; i ++)
17 17 {
18   - sum += c.sampleStandardNormalUnivariate();
  18 + sum += c.sampleNormalUnivariate(2,1);
19 19 }
20 20 System.out.println("sum = "+ sum +" ave = " + sum * 1.0 / 20000000);
21 21 return;

0 comments on commit 467fc23

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