001/* 002 * Licensed to the author under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package de.cuioss.test.generator.internal.net.java.quickcheck.generator.distribution; 018 019import static java.lang.Math.abs; 020 021/** 022 * <a href= 023 * "http://en.wikipedia.org/wiki/Image:Normal_distribution_pdf.png">Normal 024 * distribution</a> and <a href= 025 * "http://en.wikipedia.org/wiki/Image:Uniform_distribution_PDF.png">uniform 026 * distribution</a> distribution functions. 027 * 028 * @author $Id$ 029 */ 030public interface Distribution { 031 032 /** 033 * Right side of the bell curve. Values range from 0.0 to 1.0. Values near 0.0 034 * are the most probable. 035 */ 036 Distribution POSITIV_NORMAL = new AbstractDistribution() { 037 038 @Override 039 public double nextRandomNumber() { 040 return abs(nextGausian()); 041 } 042 }; 043 044 /** 045 * Left side of the bell curve. Values range from 0.0 to 1.0. Values near 1.0 046 * are the most probable. 047 */ 048 Distribution NEGATIV_NORMAL = new AbstractDistribution() { 049 050 @Override 051 public double nextRandomNumber() { 052 return abs(-1 + abs(nextGausian())); 053 } 054 }; 055 056 /** 057 * An inverted bell curve. Values range from 0.0 to 1.0. Values near 0.0 and 1.0 058 * are the most probable. 059 */ 060 Distribution INVERTED_NORMAL = new AbstractDistribution() { 061 062 @Override 063 public double nextRandomNumber() { 064 double next = nextGausian(N_SIGMA * 2); 065 return (next < 0) ? 1 + next : next; 066 } 067 }; 068 069 /** 070 * A uniform distribution function. Values range from 0.0 to 1.0. 071 */ 072 Distribution UNIFORM = new AbstractDistribution() { 073 074 @Override 075 public double nextRandomNumber() { 076 return RandomConfiguration.random.nextDouble(); 077 } 078 }; 079 080 /** 081 * Generate the next random number for this distribution function. 082 * 083 * @return double 0 <= x <= 1.0 084 */ 085 double nextRandomNumber(); 086 087 abstract class AbstractDistribution implements Distribution { 088 089 static final int N_SIGMA = 3; 090 091 double nextGausian() { 092 return nextGausian(N_SIGMA); 093 } 094 095 double nextGausian(int sigma) { 096 // n * sigma range normalized to 1.0 097 return (RandomConfiguration.random.nextGaussian() % sigma) / sigma; 098 } 099 } 100}