From 09e5cdc0dd3991e69982bce6872edf201f25e7ed Mon Sep 17 00:00:00 2001 From: Joseph Rushton Wakeling Date: Mon, 10 May 2010 09:13:10 +0200 Subject: [PATCH] Flat (uniform) distribution over open interval. * Two new functions that provide random variates and the pdf of a flat (uniform) distribution over the open interval (a, b). * Corrected a potentially misleading comment in the function for a uniform distribution on the half-closed interval [a, b). --- randist/flat.c | 33 ++++++++++++++++++++++++++++++++- randist/gsl_randist.h | 2 ++ 2 files changed, 34 insertions(+), 1 deletions(-) diff --git a/randist/flat.c b/randist/flat.c index 996366e..0a72a85 100644 --- a/randist/flat.c +++ b/randist/flat.c @@ -34,7 +34,7 @@ gsl_ran_flat (const gsl_rng * r, const double a, const double b) { double u = gsl_rng_uniform (r); - /* A uniform distribution over [a,b] */ + /* A uniform distribution over the half-open interval [a,b) */ return a * (1 - u) + b * u; } @@ -51,3 +51,34 @@ gsl_ran_flat_pdf (double x, const double a, const double b) return 0; } } + + +/* This is the uniform distribution in the range (a, b) + + p(x) dx = 1/(b-a) dx if a < x < b + ..... = 0 otherwise + + */ + +double +gsl_ran_flat_open (const gsl_rng *r, const double a, const double b) +{ + double u = gsl_rng_uniform_pos (r); + + /* A uniform distribution over the open interval (a, b) */ + + return a * (1 - u) + b * u; +} + +double +gsl_ran_flat_open_pdf (double x, const double a, const double b) +{ + if (x < b && x > a) + { + return 1 / (b - a); + } + else + { + return 0; + } +} diff --git a/randist/gsl_randist.h b/randist/gsl_randist.h index 6f4b0e3..7218c76 100644 --- a/randist/gsl_randist.h +++ b/randist/gsl_randist.h @@ -68,6 +68,8 @@ double gsl_ran_fdist_pdf (const double x, const double nu1, const double nu2); double gsl_ran_flat (const gsl_rng * r, const double a, const double b); double gsl_ran_flat_pdf (double x, const double a, const double b); +double gsl_ran_flat_open (const gsl_rng * r, const double a, const double b); +double gsl_ran_flat_open_pdf (double x, const double a, const double b); double gsl_ran_gamma (const gsl_rng * r, const double a, const double b); double gsl_ran_gamma_int (const gsl_rng * r, const unsigned int a); -- 1.7.0.4