1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/glprng02.c Mon Dec 06 13:09:21 2010 +0100
1.3 @@ -0,0 +1,76 @@
1.4 +/* glprng02.c */
1.5 +
1.6 +/***********************************************************************
1.7 +* This code is part of GLPK (GNU Linear Programming Kit).
1.8 +*
1.9 +* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
1.10 +* 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
1.11 +* Moscow Aviation Institute, Moscow, Russia. All rights reserved.
1.12 +* E-mail: <mao@gnu.org>.
1.13 +*
1.14 +* GLPK is free software: you can redistribute it and/or modify it
1.15 +* under the terms of the GNU General Public License as published by
1.16 +* the Free Software Foundation, either version 3 of the License, or
1.17 +* (at your option) any later version.
1.18 +*
1.19 +* GLPK is distributed in the hope that it will be useful, but WITHOUT
1.20 +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1.21 +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1.22 +* License for more details.
1.23 +*
1.24 +* You should have received a copy of the GNU General Public License
1.25 +* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
1.26 +***********************************************************************/
1.27 +
1.28 +#include "glpenv.h"
1.29 +#include "glprng.h"
1.30 +#define xfault xerror
1.31 +
1.32 +/***********************************************************************
1.33 +* NAME
1.34 +*
1.35 +* rng_unif_01 - obtain pseudo-random number in the range [0, 1]
1.36 +*
1.37 +* SYNOPSIS
1.38 +*
1.39 +* #include "glprng.h"
1.40 +* double rng_unif_01(RNG *rand);
1.41 +*
1.42 +* RETURNS
1.43 +*
1.44 +* The routine rng_unif_01 returns a next pseudo-random number which is
1.45 +* uniformly distributed in the range [0, 1]. */
1.46 +
1.47 +double rng_unif_01(RNG *rand)
1.48 +{ double x;
1.49 + x = (double)rng_next_rand(rand) / 2147483647.0;
1.50 + xassert(0.0 <= x && x <= 1.0);
1.51 + return x;
1.52 +}
1.53 +
1.54 +/***********************************************************************
1.55 +* NAME
1.56 +*
1.57 +* rng_uniform - obtain pseudo-random number in the range [a, b]
1.58 +*
1.59 +* SYNOPSIS
1.60 +*
1.61 +* #include "glprng.h"
1.62 +* double rng_uniform(RNG *rand, double a, double b);
1.63 +*
1.64 +* RETURNS
1.65 +*
1.66 +* The routine rng_uniform returns a next pseudo-random number which is
1.67 +* uniformly distributed in the range [a, b]. */
1.68 +
1.69 +double rng_uniform(RNG *rand, double a, double b)
1.70 +{ double x;
1.71 + if (a >= b)
1.72 + xfault("rng_uniform: a = %g, b = %g; invalid range\n", a, b);
1.73 + x = rng_unif_01(rand);
1.74 + x = a * (1.0 - x) + b * x;
1.75 + xassert(a <= x && x <= b);
1.76 + return x;
1.77 +}
1.78 +
1.79 +/* eof */