/* Copyright (c) 1997-2009
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
   http://www.polymake.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.
--------------------------------------------------------------------------------
   $Project: polymake $$Id: rand_box.cc 9523 2010-03-04 13:27:19Z herr $
*/

#include <polymake.h>
#include <Matrix.h>
#include <random_generators.h>

namespace polymake { namespace polytope {

perl::Object rand_box(int d, int n, int b, perl::OptionSet options)
{
   if (d<1 || n<1 || b<1) throw std::runtime_error("rand_box: 1 <= dim, #POINTS, b");

   UniformRNG<long> random(options["seed"] ? std::atol(options["seed"]) : random_seed());

   perl::Object p("Polytope<Rational>");

   p.take("AMBIENT_DIM") << d;
   Matrix<int> V(n,d+1);
   for (int i=0; i<n; ++i) {
      V(i,0) = 1;
      for (int k=1; k<=d; ++k) {
	 int r=random.get()%(b+1);
	 if (r<0) r+=b;
	 V(i,k)=r;
      }
   }
  
   p.take("POINTS") << V;
   return p;

}

  UserFunction4perl("# @category Producing from scratch"
		    "# Computes the convex hull of //n// points sampled uniformly at random from the"
		    "# integer points in the cube [0,//b//]<sup>//d//</sup>."
		    "# @param Int d the dimension of the box"
		    "# @param Int n the number of random points"
		    "# @param Int b the size of the box"
		    "# @option Int seed controls the outcome of the random number generator;"
		    "#   fixing a seed number guarantees the same outcome."
		    "# @return Polytope",
		    &rand_box,"rand_box($$$ { seed => undef })");
} }
