/*
	Lan Wars.
	Copyright (c) 2009, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''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 DARRAGH COY 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.
*/

//=====================================================================================================================
/// @brief		This class represents a random number generator and allows numbers to be generated randomly.
//=====================================================================================================================

#include "Precompiled.h"

using namespace Game;

//=====================================================================================================================
/// @brief		Default constructor: creates the random number generator and seeds based on current time
//=====================================================================================================================

Random::Random(){ Randomize(); }

//=====================================================================================================================
/// @brief		Randomises the random number generator. Picking a random seed
//=====================================================================================================================

void Random::Randomize()
{
	// Use the processor (or high res timers) time stamp:
	
	LARGE_INTEGER li; QueryPerformanceCounter(&li);
	
	// Set the seed:
	
	m_seed = (unsigned) li.LowPart;
}

//=====================================================================================================================
/// @brief		Overloaded constructor: creates the number generator and sets a seed manually
/// @param		seed - seed to use for the RNG
//=====================================================================================================================

Random::Random( unsigned seed )
{
	m_seed = seed;
}

//=====================================================================================================================
/// @brief		Gives a random float between 0 and 1
/// @returns	A random float between 0 and 1
//=====================================================================================================================

float Random::NextFloat()
{
	// Get an unsigned integer
	
	unsigned r = NextUnsigned();
	
	// Clamp to maximum number of possible values in mantissa of a float
	
	r &= 0x007fffff;
	
	// Divide by the maximum number of possible values in the mantissa of a float:
	
	return float(r) / float( 0x007fffff );
}

//=====================================================================================================================
/// @brief		Gives a random float between two numbers
/// @param		min - minimum random value
/// @param		max - maximum random value
/// @pre		min must be less than max, and not equal to it
/// @returns	Random float between the range specified
//=====================================================================================================================

float Random::NextFloat( float min , float max )
{
	// Make sure ok:
	
	ENSURE( min < max , "min must be less than max, and not equal to it" );
	
	// Pick a random float
	
	float t = NextFloat();
	
	// Interpolate between the two values
	
	return min * t + ( 1.0f - t ) * max;
}

//=====================================================================================================================
/// @brief		Gives a random unsigned integer. may be any value.
/// @returns	A random integer anywhere inside the signed range of an integer
//=====================================================================================================================

unsigned Random::NextUnsigned()
{
    // Random genration constants: all of these are nice prime numbers which are very good for giving the appearance of randomness:

    #define ADD	2003
    #define MUL	7919

    // Generate the number

    m_seed = m_seed * MUL + ADD;

    // Return the random number:

    return m_seed;
    
    // Undefine these
    
    #undef ADD
    #undef MUL
}

//=====================================================================================================================
/// @brief		Gives a random unsigned integer between the two values.
/// @param		min - minimum random value
/// @param		max - maximum random value
/// @pre		min must be less than max, and not equal to it
/// @returns	Random unsigned integer between the range specified
//=====================================================================================================================

unsigned Random::NextUnsigned( unsigned min , unsigned max )
{
	// Make sure ok:
	
	ENSURE( min < max , "min must be less than max, and not equal to it" );
	
	// Get a random unsigned:
	
	unsigned r = NextUnsigned();
	
	// Clamp to range given:
	
	r %= max - min;
	
	// Bring up to minimum value
	
	r += min;
	
	// Return the integer
	
	return r;
}

//=====================================================================================================================
/// @brief		Gives a random signed integer. may be any value.
/// @returns	Random integer of any value
//=====================================================================================================================

int Random::NextInt()
{
	return (int)(NextUnsigned());
}

//=====================================================================================================================
/// @brief		Gives a signed integer between the two given values
/// @param		min - minimum random value
/// @param		max - maximum random value
/// @pre		min must be less than max, and not equal to it
//=====================================================================================================================

int Random::NextInt( int min , int max )
{
	// Make sure ok:
	
	ENSURE( min < max , "min must be less than max, and not equal to it" );
	
	// Get a random unsigned:
	
	unsigned r = NextUnsigned();
	
	// Clamp to range given:
	
	r %= (unsigned)( max - min );
	
	// Bring up to minimum value and return
	
	return ((int)(r)) + min;
}
