/* Copyright (c) 1997-2005
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
*/

#ident "$Project: polymake $$Id: random_generators.cc,v 1.7 2005/09/22 17:12:12 gawrilow Exp $"

#if defined(__GNUC__) && __GNUC__==3 && __GNUC_MINOR__<3 && !defined(__APPLE__) && !defined(__COMO__)
#pragma implementation
#endif
#include <random_generators.h>

namespace pm {

void NormalRNG::fill()
{
   double v, u, s;
   do {
      v = 2*super::get()-1;
      u = 2*super::get()-1;
      s = u*u + v*v;
   } while (s >= 1);

   double scale = sqrt( (-2*log(s)) / s );
   x[0]=v * scale;
   x[1]=u * scale;
   index=0;
}

void UniformRNG< Vector<double> >::fill_vector() {
   double norm;
   do {
      std::copy(NormalRNG::begin(), entire(point));
      norm = sqr(point);
   } while (norm == 0.0);	 // this occurs with very low probability
   point /= sqrt(norm);
}

} // end namespace pm
