1 /* randist/gsl-randist.c
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include <gsl/gsl_randist.h>
27 #include <gsl/gsl_rng.h>
28 #include <gsl/gsl_test.h>
30 void error (const char * s);
34 main (int argc, char *argv[])
38 double mu = 0, nu = 0, nu1 = 0, nu2 = 0, sigma = 0, a = 0, b = 0, c = 0;
39 double zeta = 0, sigmax = 0, sigmay = 0, rho = 0;
41 double x = 0, y =0, z=0 ;
42 unsigned int N = 0, t = 0, n1 = 0, n2 = 0 ;
43 unsigned long int seed = 0 ;
50 "Usage: gsl-randist seed n DIST param1 param2 ...\n"
51 "Generates n samples from the distribution DIST with parameters param1,\n"
52 "param2, etc. Valid distributions are,\n\n");
57 " bivariate-gaussian\n"
82 " negative-binomial\n"
95 argv++ ; seed = atol (argv[0]); argc-- ;
96 argv++ ; n = atol (argv[0]); argc-- ;
97 argv++ ; name = argv[0] ; argc-- ; argc-- ;
101 if (gsl_rng_default_seed != 0) {
103 "overriding GSL_RNG_SEED with command line value, seed = %ld\n",
107 gsl_rng_default_seed = seed ;
109 r = gsl_rng_alloc(gsl_rng_default) ;
112 #define NAME(x) !strcmp(name,(x))
113 #define OUTPUT(x) for (i = 0; i < n; i++) { printf("%g\n", (x)) ; }
114 #define OUTPUT1(a,x) for(i = 0; i < n; i++) { a ; printf("%g\n", x) ; }
115 #define OUTPUT2(a,x,y) for(i = 0; i < n; i++) { a ; printf("%g %g\n", x, y) ; }
116 #define OUTPUT3(a,x,y,z) for(i = 0; i < n; i++) { a ; printf("%g %g %g\n", x, y, z) ; }
117 #define INT_OUTPUT(x) for (i = 0; i < n; i++) { printf("%d\n", (x)) ; }
118 #define ARGS(x,y) if (argc != x) error(y) ;
119 #define DBL_ARG(x) if (argc) { x=atof((++argv)[0]);argc--;} else {error( #x);};
120 #define INT_ARG(x) if (argc) { x=atoi((++argv)[0]);argc--;} else {error( #x);};
122 if (NAME("bernoulli"))
124 ARGS(1, "p = probability of success");
126 INT_OUTPUT(gsl_ran_bernoulli (r, p));
128 else if (NAME("beta"))
130 ARGS(2, "a,b = shape parameters");
133 OUTPUT(gsl_ran_beta (r, a, b));
135 else if (NAME("binomial"))
137 ARGS(2, "p = probability, N = number of trials");
140 INT_OUTPUT(gsl_ran_binomial (r, p, N));
142 else if (NAME("cauchy"))
144 ARGS(1, "a = scale parameter");
146 OUTPUT(gsl_ran_cauchy (r, a));
148 else if (NAME("chisq"))
150 ARGS(1, "nu = degrees of freedom");
152 OUTPUT(gsl_ran_chisq (r, nu));
154 else if (NAME("erlang"))
156 ARGS(2, "a = scale parameter, b = order");
159 OUTPUT(gsl_ran_erlang (r, a, b));
161 else if (NAME("exponential"))
163 ARGS(1, "mu = mean value");
165 OUTPUT(gsl_ran_exponential (r, mu));
167 else if (NAME("exppow"))
169 ARGS(2, "a = scale parameter, b = power (1=exponential, 2=gaussian)");
172 OUTPUT(gsl_ran_exppow (r, a, b));
174 else if (NAME("fdist"))
176 ARGS(2, "nu1, nu2 = degrees of freedom parameters");
179 OUTPUT(gsl_ran_fdist (r, nu1, nu2));
181 else if (NAME("flat"))
183 ARGS(2, "a = lower limit, b = upper limit");
186 OUTPUT(gsl_ran_flat (r, a, b));
188 else if (NAME("gamma"))
190 ARGS(2, "a = order, b = scale");
193 OUTPUT(gsl_ran_gamma (r, a, b));
195 else if (NAME("gaussian"))
197 ARGS(1, "sigma = standard deviation");
199 OUTPUT(gsl_ran_gaussian (r, sigma));
201 else if (NAME("gaussian-tail"))
203 ARGS(2, "a = lower limit, sigma = standard deviation");
206 OUTPUT(gsl_ran_gaussian_tail (r, a, sigma));
208 else if (NAME("ugaussian"))
210 ARGS(0, "unit gaussian, no parameters required");
211 OUTPUT(gsl_ran_ugaussian (r));
213 else if (NAME("ugaussian-tail"))
215 ARGS(1, "a = lower limit");
217 OUTPUT(gsl_ran_ugaussian_tail (r, a));
219 else if (NAME("bivariate-gaussian"))
221 ARGS(3, "sigmax = x std.dev., sigmay = y std.dev., rho = correlation");
225 OUTPUT2(gsl_ran_bivariate_gaussian (r, sigmax, sigmay, rho, &x, &y),
228 else if (NAME("dir-2d"))
230 OUTPUT2(gsl_ran_dir_2d (r, &x, &y), x, y);
232 else if (NAME("dir-3d"))
234 OUTPUT3(gsl_ran_dir_3d (r, &x, &y, &z), x, y, z);
236 else if (NAME("dir-nd"))
239 ARGS(1, "n1 = number of dimensions of hypersphere");
241 xarr = (double *)malloc(n1*sizeof(double));
243 for(i = 0; i < n; i++) {
244 gsl_ran_dir_nd (r, n1, xarr) ;
245 for (j = 0; j < n1; j++) {
247 printf("%g", xarr[j]) ;
254 else if (NAME("geometric"))
256 ARGS(1, "p = bernoulli trial probability of success");
258 INT_OUTPUT(gsl_ran_geometric (r, p));
260 else if (NAME("gumbel1"))
262 ARGS(2, "a = order, b = scale parameter");
265 OUTPUT(gsl_ran_gumbel1 (r, a, b));
267 else if (NAME("gumbel2"))
269 ARGS(2, "a = order, b = scale parameter");
272 OUTPUT(gsl_ran_gumbel2 (r, a, b));
274 else if (NAME("hypergeometric"))
276 ARGS(3, "n1 = tagged population, n2 = untagged population, t = number of trials");
280 INT_OUTPUT(gsl_ran_hypergeometric (r, n1, n2, t));
282 else if (NAME("laplace"))
284 ARGS(1, "a = scale parameter");
286 OUTPUT(gsl_ran_laplace (r, a));
288 else if (NAME("landau"))
290 ARGS(0, "no arguments required");
291 OUTPUT(gsl_ran_landau (r));
293 else if (NAME("levy"))
295 ARGS(2, "c = scale, a = power (1=cauchy, 2=gaussian)");
298 OUTPUT(gsl_ran_levy (r, c, a));
300 else if (NAME("levy-skew"))
302 ARGS(3, "c = scale, a = power (1=cauchy, 2=gaussian), b = skew");
306 OUTPUT(gsl_ran_levy_skew (r, c, a, b));
308 else if (NAME("logarithmic"))
310 ARGS(1, "p = probability");
312 INT_OUTPUT(gsl_ran_logarithmic (r, p));
314 else if (NAME("logistic"))
316 ARGS(1, "a = scale parameter");
318 OUTPUT(gsl_ran_logistic (r, a));
320 else if (NAME("lognormal"))
322 ARGS(2, "zeta = location parameter, sigma = scale parameter");
325 OUTPUT(gsl_ran_lognormal (r, zeta, sigma));
327 else if (NAME("negative-binomial"))
329 ARGS(2, "p = probability, a = order");
332 INT_OUTPUT(gsl_ran_negative_binomial (r, p, a));
334 else if (NAME("pareto"))
336 ARGS(2, "a = power, b = scale parameter");
339 OUTPUT(gsl_ran_pareto (r, a, b));
341 else if (NAME("pascal"))
343 ARGS(2, "p = probability, n = order (integer)");
346 INT_OUTPUT(gsl_ran_pascal (r, p, N));
348 else if (NAME("poisson"))
350 ARGS(1, "mu = scale parameter");
352 INT_OUTPUT(gsl_ran_poisson (r, mu));
354 else if (NAME("rayleigh"))
356 ARGS(1, "sigma = scale parameter");
358 OUTPUT(gsl_ran_rayleigh (r, sigma));
360 else if (NAME("rayleigh-tail"))
362 ARGS(2, "a = lower limit, sigma = scale parameter");
365 OUTPUT(gsl_ran_rayleigh_tail (r, a, sigma));
367 else if (NAME("tdist"))
369 ARGS(1, "nu = degrees of freedom");
371 OUTPUT(gsl_ran_tdist (r, nu));
373 else if (NAME("weibull"))
375 ARGS(2, "a = scale parameter, b = exponent");
378 OUTPUT(gsl_ran_weibull (r, a, b));
382 fprintf(stderr,"Error: unrecognized distribution: %s\n", name) ;
390 error (const char * s)
392 fprintf(stderr, "Error: arguments should be %s\n",s) ;
393 exit (EXIT_FAILURE) ;