/*
**  random.h
**
**  Copyright (c) 1997-2001, Joel D. Kraft
**  Electrical Engineering & Computer Science
**  Case Western Reserve University
**
**  Questions or comments should be directed to jdk6@po.cwru.edu.
**
**  Version 1.1.2: 21 April 2001
**
**  This file provides two random number generating functions.
**
**  int random(int lower, int upper)
**      This function returns a random number between lower and
**		upper, inclusively.  The value of upper must be greater
**		than lower. The pseudo-random number generator will be
**		automatically initialized using the system clock the first
**		time the function is called.
**
**  int random(int upper)
**      This function returns a random number between 1 and upper,
**		inclusively.  It is identical to calling random(1, upper).
**
**	These functions are contained in an unnamed namespace so that
**	this file can be safely included in multiple compilation units
**	in the same project.  This is done only to make it easier for
**	beginning students to use the library without having to worry
**	about complicated linking issues.  As a result of this, the
**	random number generator may be seeded multiple times if it is
**	indeed included and used in multiple compilation units.
**
--------------------------------------------------------------------------
*/

#ifndef __JDK6_RANDOM
#define __JDK6_RANDOM

#include <cstdlib>
#include <ctime>
#include <cassert>

namespace {

//------------------------------------------------------------------------

int random(int lower, int upper);
int random(int upper);

//------------------------------------------------------------------------

int random(int lower, int upper)
{
	static bool seeded = false;
	assert(lower < upper);

	//checks if seeded, if not, seeds, then sets to true
	if (!seeded)
	{
		srand(long(time(0)));
		seeded = true;
	}

	return (rand() % (upper - lower + 1)) + lower;
}

int random(int upper)
{
	return random(1, upper);
}

//------------------------------------------------------------------------

}

#endif

/*
--------------------------------------------------------------------------
**
**  Version History
**
**  1.0    01-Dec-97  Initial Release
**  1.1    09-Mar-00  Updated Namespace Usage
**  1.1.1  20-Sep-00  Use jdk Namespace
**	1.1.2  21-Apr-01  Use Unnamed Namespace for Multiple Source Files
**                    Remove Non-negative Restriction
**
*/
