| 1 | /* glprng02.c */ |
| 2 | |
| 3 | /*********************************************************************** |
| 4 | * This code is part of GLPK (GNU Linear Programming Kit). |
| 5 | * |
| 6 | * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, |
| 7 | * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, |
| 8 | * Moscow Aviation Institute, Moscow, Russia. All rights reserved. |
| 9 | * E-mail: <[email protected]>. |
| 10 | * |
| 11 | * GLPK is free software: you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation, either version 3 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * GLPK is distributed in the hope that it will be useful, but WITHOUT |
| 17 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 18 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 19 | * License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with GLPK. If not, see <http://www.gnu.org/licenses/>. |
| 23 | ***********************************************************************/ |
| 24 | |
| 25 | #include "glpenv.h" |
| 26 | #include "glprng.h" |
| 27 | #define xfault xerror |
| 28 | |
| 29 | /*********************************************************************** |
| 30 | * NAME |
| 31 | * |
| 32 | * rng_unif_01 - obtain pseudo-random number in the range [0, 1] |
| 33 | * |
| 34 | * SYNOPSIS |
| 35 | * |
| 36 | * #include "glprng.h" |
| 37 | * double rng_unif_01(RNG *rand); |
| 38 | * |
| 39 | * RETURNS |
| 40 | * |
| 41 | * The routine rng_unif_01 returns a next pseudo-random number which is |
| 42 | * uniformly distributed in the range [0, 1]. */ |
| 43 | |
| 44 | double rng_unif_01(RNG *rand) |
| 45 | { double x; |
| 46 | x = (double)rng_next_rand(rand) / 2147483647.0; |
| 47 | xassert(0.0 <= x && x <= 1.0); |
| 48 | return x; |
| 49 | } |
| 50 | |
| 51 | /*********************************************************************** |
| 52 | * NAME |
| 53 | * |
| 54 | * rng_uniform - obtain pseudo-random number in the range [a, b] |
| 55 | * |
| 56 | * SYNOPSIS |
| 57 | * |
| 58 | * #include "glprng.h" |
| 59 | * double rng_uniform(RNG *rand, double a, double b); |
| 60 | * |
| 61 | * RETURNS |
| 62 | * |
| 63 | * The routine rng_uniform returns a next pseudo-random number which is |
| 64 | * uniformly distributed in the range [a, b]. */ |
| 65 | |
| 66 | double rng_uniform(RNG *rand, double a, double b) |
| 67 | { double x; |
| 68 | if (a >= b) |
| 69 | xfault("rng_uniform: a = %g, b = %g; invalid range\n", a, b); |
| 70 | x = rng_unif_01(rand); |
| 71 | x = a * (1.0 - x) + b * x; |
| 72 | xassert(a <= x && x <= b); |
| 73 | return x; |
| 74 | } |
| 75 | |
| 76 | /* eof */ |