1/** *//** 2 * 3*/ 4package algorithm.math; 5 6import org.apache.commons.math.random.RandomData; 7import org.apache.commons.math.random.RandomDataImpl; 8import org.apache.commons.math.stat.Frequency; 9import org.apache.commons.math.stat.StatUtils; 10 11/** *//** 12 * @author Jia Yu 13 * @date 2010-12-4 14*/ 15publicclass RandomTest { 16 17/** *//** 18 * @param args 19*/ 20publicstaticvoid main(String[] args) { 21// TODO Auto-generated method stub 22 random(); 23 } 24 25privatestaticvoid random() { 26// TODO Auto-generated method stub 27 RandomData randomData =new RandomDataImpl(); 28 29//Generate a random int value uniformly distributed between lower and upper, inclusive 30 System.out.println("a uniform value: "+randomData.nextInt(1, 6)); 31//Returns a random value from an Exponential distribution with the given mean 32 System.out.println("a Exponential value: "+randomData.nextExponential(5)); 33//Generate a random value from a Normal 34 System.out.println("a Normal value: "+randomData.nextGaussian(0, 1)); 35//Generates a random value from the Poisson distribution with the given mean 36 System.out.println("a Poisson value: "+randomData.nextPoisson(3)); 37//Generates an integer array of length k whose entries are selected randomly, without repetition, from the integers 0 through n-1 38int[] a = randomData.nextPermutation(10, 3); 39for(int i=0;i<a.length;i++){ 40 System.out.print(a[i]+""); 41 } 42 System.out.println(); 43 44//generate 1000 numbers between 0 and 3 inclusive, then using frequency to see the distribution 45 Frequency freq =new Frequency(); 46int value =0; 47for (int i =0; i <1000; i++) { 48 value = randomData.nextInt(0, 3); 49 freq.addValue(value); 50 } 51long[] observed =newlong[4]; 52double[] perc =newdouble[4]; 53for (int i =0; i <4; i++) { 54 observed[i] = freq.getCount(i); 55 perc[i] = freq.getPct(i); 56 System.out.println("there are "+observed[i]+""+i+" in dataset with "+(perc[i]*100)+"%"); 57 } 58 59//stat test 60double[] data ={1d,2d,2d,3d}; 61 System.out.println("sum of data is "+StatUtils.sum(data)); 62 System.out.println("sum of square of data is "+StatUtils.sumSq(data)); 63 System.out.println("var of data is "+StatUtils.variance(data)); 64 System.out.println("mean of data is "+StatUtils.mean(data)); 65 System.out.println("max value of data is "+StatUtils.max(data)); 66 System.out.println("min value of data is "+StatUtils.min(data)); 67 System.out.println("geometry mean of data is "+StatUtils.geometricMean(data)); 68 System.out.println("product of data is "+StatUtils.product(data)); 69 } 70 71 } 72
看看结果: a uniform value: 6 a Exponential value: 3.4850055739123356 a Normal value: -0.510139146901537 a Poisson value: 4 1 4 3 there are 266 0 in dataset with 26.6% there are 234 1 in dataset with 23.400000000000002% there are 240 2 in dataset with 24.0% there are 260 3 in dataset with 26.0% sum of data is 8.0 sum of square of data is 18.0 var of data is 0.6666666666666666 mean of data is 2.0 max value of data is 3.0 min value of data is 1.0 geometry mean of data is 1.8612097182041993 product of data is 12.0