javaで正規分布

戻る

::::::::::::::
GaussianTest.java
::::::::::::::
/**
* $Id: Gaussian.html,v 1.1 2009/06/22 16:11:44 kishi Exp kishi $
*/
import java.util.*;

public class GaussianTest {

    public static void main ( String[] args ) {

        int count = 1000000;
        List list = new LinkedList();
        Random random = new Random();

        double max = Double.MIN_VALUE;
        double min = Double.MAX_VALUE;
        for ( int i = 0;i < count;i++ ) {
            // System.out.println( random.nextGaussian() );

            double value = random.nextGaussian();
            list.add ( value );

            if ( value < min ) {
                min = value;
            }
            if ( value > max ) {
                max = value;
            }
        }

        System.out.println ( "max=" + max );
        System.out.println ( "min=" + min );

        double range = max - min;
        int mapSize = 100;
        double zoneWidth = range/mapSize;

        Map occurrenceMap = new HashMap();
        Iterator iterator = list.iterator();
        int i = 0;
        while ( iterator.hasNext() ) {

            Double value = ( Double ) iterator.next();
            double offset = value.doubleValue() - min;
            int rank = ( int ) ( offset /zoneWidth );

            if ( !occurrenceMap.containsKey ( rank ) ) {
                occurrenceMap.put ( rank, 0 );
            }
            Integer frequency = ( Integer ) occurrenceMap.get ( rank );
            occurrenceMap.put ( rank, frequency.intValue() + 1 );

        }

        //-------------------------------------------------------
        // Displays the distribution
        //-------------------------------------------------------
        for ( int rank = 0;rank <= 100;rank++ ) {
            Integer frequency = ( Integer ) occurrenceMap.get ( rank );
            System.out.printf ( "%3d: %6d\n" , rank, frequency == null ? 0 : frequency.intValue() );
        }

    }

}

/**
$ javap java.lang.Double
Compiled from "Double.java"
public final class java.lang.Double extends java.lang.Number implements java.lang.Comparable{
    public static final double POSITIVE_INFINITY;
    public static final double NEGATIVE_INFINITY;
    public static final double NaN;
    public static final double MAX_VALUE;
    public static final double MIN_VALUE;
    public static final int SIZE;
    public static final java.lang.Class TYPE;
    public static java.lang.String toString(double);
    public static java.lang.String toHexString(double);
    public static java.lang.Double valueOf(java.lang.String)       throws java.lang.NumberFormatException;
    public static java.lang.Double valueOf(double);
    public static double parseDouble(java.lang.String)       throws java.lang.NumberFormatException;
    public static boolean isNaN(double);
    public static boolean isInfinite(double);
    public java.lang.Double(double);
    public java.lang.Double(java.lang.String)       throws java.lang.NumberFormatException;
    public boolean isNaN();
    public boolean isInfinite();
    public java.lang.String toString();
    public byte byteValue();
    public short shortValue();
    public int intValue();
    public long longValue();
    public float floatValue();
    public double doubleValue();
    public int hashCode();
    public boolean equals(java.lang.Object);
    public static native long doubleToLongBits(double);
    public static native long doubleToRawLongBits(double);
    public static native double longBitsToDouble(long);
    public int compareTo(java.lang.Double);
    public static int compare(double, double);
    public int compareTo(java.lang.Object);
    static {};
}
*/

■実行結果

max=4.8814417167071245
min=-4.51457532363056
  0:      3
  1:      2
  2:      3
  3:      5
  4:     10
  5:      9
  6:     22
  7:     23
  8:     38
  9:     52
 10:     70
 11:    108
 12:    136
 13:    177
 14:    265
 15:    342
 16:    480
 17:    651
 18:    831
 19:   1032
 20:   1208
 21:   1603
 22:   2047
 23:   2719
 24:   3268
 25:   3909
 26:   4851
 27:   5766
 28:   6938
 29:   8018
 30:   9879
 31:  11170
 32:  12961
 33:  14800
 34:  16575
 35:  18583
 36:  20911
 37:  23168
 38:  24896
 39:  27069
 40:  29075
 41:  30820
 42:  32680
 43:  34166
 44:  35588
 45:  36516
 46:  37115
 47:  37374
 48:  37711
 49:  37169
 50:  36441
 51:  35660
 52:  34728
 53:  32821
 54:  31397
 55:  29219
 56:  27297
 57:  24994
 58:  22973
 59:  20840
 60:  19049
 61:  16823
 62:  14924
 63:  13130
 64:  11361
 65:   9601
 66:   8296
 67:   7223
 68:   5916
 69:   4937
 70:   4043
 71:   3347
 72:   2768
 73:   2080
 74:   1698
 75:   1313
 76:   1010
 77:    810
 78:    620
 79:    504
 80:    369
 81:    271
 82:    220
 83:    143
 84:    114
 85:     61
 86:     58
 87:     39
 88:     26
 89:     28
 90:     15
 91:      6
 92:      4
 93:      6
 94:      2
 95:      0
 96:      2
 97:      0
 98:      0
 99:      0
100:      1


戻る inserted by FC2 system