glprng02.c 2.3 KB

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