001/* 002 * RandomExponentialDistribution.java 003 * Copyright (c) 2002 004 * 005 * This program is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU General Public License 007 * as published by the Free Software Foundation; either version 2 008 * of the License, or any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 018 */ 019 020 021package ca.bc.webarts.widgets.random; 022 023import java.util.Random; 024 025/** 026 * 027 * A generator of random numbers with exponential distribution. 028 * 029 * (C) 2000, Laurentiu Cristofor (laur72_98@yahoo.com) 030 * 031 **/ 032public class RandomExponentialDistribution 033{ 034 private Random rand; 035 private double mean; 036 037 /** 038 * Create a new generator of random numbers 039 * with exponential distribution of mean 1. 040 */ 041 public RandomExponentialDistribution() 042 { 043 this(1.0); 044 } 045 046 /** 047 * Create a new generator of random numbers 048 * with exponential distribution of specified mean. 049 * 050 * @param mean the mean of the exponential distribution 051 */ 052 public RandomExponentialDistribution(double mean) 053 { 054 this.mean = mean; 055 rand = new Random(); 056 } 057 058 /** 059 * Return a random number with exponential distribution. 060 */ 061 public double nextDouble() 062 { 063 double val; 064 065 do 066 val = rand.nextDouble(); 067 while (val == 0.0); 068 069 return mean * (-Math.log(val)); 070 } 071 072 public static void main(String[] args) 073 { 074 RandomExponentialDistribution exponential 075 = new RandomExponentialDistribution(); 076 077 for (int i = 0; i <= 10; i++) 078 System.out.println(exponential.nextDouble()); 079 } 080} 081 082