/* -*-	Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
/*
 * Copyright (c) 1997 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the Computer Systems
 *	Engineering Group at Lawrence Berkeley Laboratory.
 * 4. Neither the name of the University nor of the Laboratory may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/tools/rng.h,v 1.24 2003/12/17 21:56:15 haldar Exp $ (LBL)";
 */

/***********************************************************************\ 
* 
* File: RngStream.h for multiple streams of Random Numbers 
* Language: C++ 
* Copyright: Pierre L'Ecuyer, University of Montreal 
* Notice: This code can be used freely for personnal, academic, 
* or non-commercial purposes. For commercial purposes, 
* please contact P. L'Ecuyer at: lecuyer@iro.umontreal.ca 
* Date: 14 August 2001 
* 
* Incorporated into rng.h and modified to maintain backward 
* compatibility with ns-2.1b8.  Users can use their current scripts 
* unmodified with the new RNG.  To get the same results as with the 
* previous RNG, define OLD_RNG when compiling (e.g., make -D OLD_RNG).
* - Michele Weigle, University of North Carolina (mcweigle@cs.unc.edu)
* October 10, 2001
*
* Changed to single stream -- uses the one of Akaroa2
* Andreas Koepke <koepke@tkn.tu-berlin.de>
* 25 May 2004
**********************************************************************/

/* new random number generator */

#ifndef _rng_h_
#define _rng_h_


#ifdef rng_stand_alone
#define stand_alone
#define rng_test
#endif

#include <math.h>
#include <stdlib.h>			// for atoi

#ifndef stand_alone
#include "config.h"
#endif   /* stand_alone */

#ifndef MAXINT
#define	MAXINT	2147483647	// XX [for now]
#endif

/*
 * RNGImplementation is internal---do not use it, use RNG.
 */
class RNGImplementation {
public:
	RNGImplementation(long seed = 1L) {
		seed_ = seed;
	};
	void set_seed(long seed) { seed_ = seed; }
	long seed() { return seed_; }
	virtual long next();
	virtual double next_double();

protected:
	long seed_;
};

/*
 * Use class RNG in real programs.
 */
class RNG 
#ifndef stand_alone
	: public TclObject 
#endif  /* stand_alone */
{

public:
	enum RNGSources { RAW_SEED_SOURCE, PREDEF_SEED_SOURCE, HEURISTIC_SEED_SOURCE };

	void init();
	static void setRNGImplementation(RNGImplementation *imp);
	~RNG();  // RNG owns a dynamic object

#ifdef OLD_RNG
	inline int seed() { return stream_->seed(); }
#else
	RNG(const char* name = "") {init();};
	RNG(long seed) {init();};
	inline long seed() {return stream_->seed();};
	void set_seed (long seed){stream_->set_seed(seed); return;};
	inline long next() {return stream_->next();};
	inline double next_double() {return stream_->next_double();};
#endif /* OLD_RNG */

	RNG(RNGSources source, int seed = 1) { init(); };
	void set_seed(RNGSources source, int seed = 1){stream_->set_seed(seed); return;};
	inline static RNG* defaultrng() { return (default_); }

#ifndef OLD_RNG
	/*
	 * Added for new RNG
	 */
	void reset_start_substream () {return;}; 
	void reset_next_substream ()  {return; }; 
	void get_state (unsigned long seed[6]) const {return;};
#endif /* !OLD_RNG */

#ifndef stand_alone
	int command(int argc, const char*const* argv);
#endif  /* stand_alone */

	// These are primitive but maybe useful.
	inline int uniform_positive_int() {  // range [0, MAXINT]
		return (int)(next());
	}
	inline double uniform_double() { // range [0.0, 1.0)
		return next_double();
	}

	// these are for backwards compatibility
 	// don't use them in new code
	inline int random() { return uniform_positive_int(); }
	inline double uniform() {return uniform_double();}

	// these are probably what you want to use
	inline int uniform(int k) 
	{ return (uniform_positive_int() % (unsigned)k); }
	inline double uniform(double r) 
	{ return (r * uniform());}
	inline double uniform(double a, double b)
	{ return (a + uniform(b - a)); }
	inline double exponential()
	{ return (-log(uniform())); }
	inline double exponential(double r)
	{ return (r * exponential());}
	// See "Wide-area traffic: the failure of poisson modeling", Vern 
	// Paxson and Sally Floyd, IEEE/ACM Transaction on Networking, 3(3),
	// pp. 226-244, June 1995, on characteristics of counting processes 
	// with Pareto interarrivals.
	inline double pareto(double scale, double shape) { 
		// When 1 < shape < 2, its mean is scale**shape, its 
		// variance is infinite.
		return (scale * (1.0/pow(uniform(), 1.0/shape)));
	}
        inline double paretoII(double scale, double shape) { 
		return (scale * ((1.0/pow(uniform(), 1.0/shape)) - 1));
	}
	double normal(double avg, double std);
	inline double lognormal(double avg, double std) { 
		return (exp (normal(avg, std))); 
	}

protected:   // need to be public?

	static RNGImplementation *stream_;
	static RNG* default_;
        static int refCount;  // needed to clean up the stream_
}; 

/*
 * Create an instance of this class to test RNGImplementation.
 * Do .verbose() for even more.
 */
#ifdef rng_test
class RNGTest {
public:
	RNGTest();
	void verbose();
	void verbose_mil();
	void first_n(RNG::RNGSources source, long seed, int n);
	void first_n_mil(RNG::RNGSources source, long seed, int n, FILE *out);
};
#endif /* rng_test */

#endif /* _rng_h_ */
