#ifndef _xbRANDOM_
#define _xbRANDOM_
/*<<<***************************************************************************
The MIT License (MIT)

Copyright(C) 2013 - 2015 Marc Mirti Mancinelli, Christophe Lardau and Philippe Pamart (xdcube.org)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
#Title: xbRandom
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<13/11/2010: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifndef _xbENGINE_
#	include "xbEngine.h"
#endif

class xbEXPORT __declspec(uuid("{8C9BB171-A9EC-44b0-8A81-29FC681DC080}"))
	xbRanrotWGenerator
{
	enum constants 
	{                     // define parameters
		KK = 17, JJ = 10, R1 = 19, R2 =  27
	};
public:
	xbRanrotWGenerator()
	{
		Architecture = archNotInit;
	}

	void RandomInit(ulong seed);        // initialization
	inline long RandomLong(long min, long max)
	{
		return RandomInt32(min,max);
	}
	long RandomInt32(long min, long max);       // get integer random number in desired interval
	int64 RandomInt64(int64 min, int64 max);    // get integer random number in desired interval
	double RandomDouble(double min,double max);						// get floating point random number
	double RandomDouble();						// get floating point random number

	ulong BRandom();                    // output random bits  

protected:
	long p1, p2;                          // indexes into buffer
	union 
	{                              // used for conversion to float
		double randp1;
		ulong randbits[3];
	};  
	ulong randbuffer[KK][2];            // history buffer
	ulong randbufcopy[KK*2][2];         // used for self-test
	
	enum TArch {archNotInit, archLITTLEENDIAN, archBIGENDIAN, archNONIEEE, archEXTENDEDPRECISIONLITTLEENDIAN};
	TArch Architecture;                  // conversion to float depends on computer architecture
};

extern xbEXPORT xbRanrotWGenerator g_xbRand;

#endif
