⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 garandom.h

📁 遗传算法做的排课系统
💻 H
字号:

#ifndef __GA_RANDOM_H__
#define __GA_RANDOM_H__

#include "..\ExportImport.h"
#include "..\CallConvention.h"
#include "..\Threading\GaThreading.h"

namespace Common
{
	// Parameters for random generator
	enum GaRandomParams
	{
		GRP_BUFFER_SIZE = 17, 
		GRP_OFFSET = 10, 
		GRP_ROTATION_1 = 19, 
		GRP_ROTATION_2 = 27
	};

	// Generator of random bits
	class GaRandomGenerator
	{

		DEFINE_SYNC_CLASS

	private:

		// Generator's buffer
		unsigned int _buffer[ 2 ][ GRP_BUFFER_SIZE ];

		// Positions in buffer
		int _positions[ 2 ];

		// TRUE if the architecture is little endian, FALSE for little endian arhitectures
		bool _littleEndian;

	public:

		// Initialize random generator with current time as seed.
		DLL_EXPORT
		GaRandomGenerator();

		// Initialize random generator with user defined seed
		DLL_EXPORT
		GaRandomGenerator(unsigned long seed);

		// Generator of random bits
		DLL_EXPORT
		void Generate(unsigned int& word1,
			unsigned int& word2);

		// Generate random floatin point number in interval 0..1
		DLL_EXPORT
		double GenerateDouble();

		// Initialization of random generator
		DLL_EXPORT
		void Initalization(unsigned int seed);

	};// END CLASS DEFINITION GaRandomGenerator

	// Base class for random generators
	template <typename TYPE>
	class GaRandom
	{

	public:

		// Generate value with no defined range
		virtual TYPE Generate()=0;

		// Generate random number with defined max bound
		virtual TYPE Generate(const TYPE& max)=0;

		// Generate random value within defined bounds
		virtual TYPE Generate(const TYPE& min,
			const TYPE& max)=0;

	};// END CLASS DEFINITION GaRandom

	// Integer number random generator
	class GaRandomInteger : public GaRandom<int>
	{

	private:

		// Generator of random bits
		GaRandomGenerator _generator;

	public:

		// Initialize random generator with current time as seed
		DLL_EXPORT
		GaRandomInteger();

		// Initialize random generator with user defined seed
		DLL_EXPORT
		GaRandomInteger(unsigned long seed);

		// Generate value with no defined range
		DLL_EXPORT
		virtual int Generate();

		// Generate random number with defined max bound
		DLL_EXPORT
		virtual int Generate(const int& max);

		// Generate random value within defined bounds
		DLL_EXPORT
		virtual int Generate(const int& min,
			const int& max);

	};// END CLASS DEFINITION GaRandomInteger

	// Floating number random generator in interval 0..1
	class GaRandomDouble : public GaRandom<double>
	{

	private:

		// Generator of random bits
		GaRandomGenerator _generator;

	public:

		// Initialize random generator with user defined seed
		DLL_EXPORT
		GaRandomDouble();

		// Initialize random generator with current time as seed
		DLL_EXPORT
		GaRandomDouble(unsigned long seed);

		// Generate value with no defined range
		DLL_EXPORT
		virtual double Generate();

		// Generate random number with defined max bound
		DLL_EXPORT
		virtual double Generate(const double& max);

		// Generate random value within defined bounds
		DLL_EXPORT
		virtual double Generate(const double& min,
			const double& max);

	};// END CLASS DEFINITION GaRandomDouble

	// Boolean random generator
	class GaRandomBool : public GaRandom<bool>
	{

	private:

		// Generator of random bits
		GaRandomGenerator _generator;

	public:

		// Initialize random generator with current time as seed
		DLL_EXPORT
		GaRandomBool();

		// Initialize random generator with user defined seed
		DLL_EXPORT
		GaRandomBool(unsigned long seed);

		// Generate value with no defined range
		DLL_EXPORT
		virtual bool Generate();

		// Generate random number with defined max bound
		DLL_EXPORT
		virtual bool Generate(const bool& max);

		// Generate random value within defined bounds
		DLL_EXPORT
		virtual bool Generate(const bool& min,
			const bool& max);

		// Generates boolean with p probability of TRUE and 1-p probability of FALSE
		DLL_EXPORT
		bool Generate(double p);

		// Generates boolean with p probability of TRUE and 100-p probability of FALSE. p is expressed in percents
		DLL_EXPORT
		bool Generate(int p);

	};// END CLASS DEFINITION GaRandomBool

} // Common

#endif // __GA_RANDOM_H__

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -