📄 random.h
字号:
/*! \file Random.h
\brief This file contains declaration of interfaces, classes and datatypes of random number generators.
*/
/*
*
* website: http://www.coolsoft-sd.com/
* contact: support@coolsoft-sd.com
*
*/
/*
* Genetic Algorithm Library
* Copyright (C) 2007-2008 Coolsoft Software Development
* Copyright (C) 1997-2007 Agner Fog
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef __GA_RANDOM_H__
#define __GA_RANDOM_H__
#include <time.h>
#include "Platform.h"
#include "Threading.h"
namespace Common
{
/// <summary>This union is used for conversation from 32-bits long integer random number
/// to single precision floating point number in interval (0, 1).</summary>
union GaUnsignedIntToFloat
{
/// <summary>This field is used to store 32-bit long integer number.</summary>
unsigned int bits;
/// <summary>field is used to read 32-bit long integer number as mantissa of single precision floating point number.</summary>
float number;
};
/// <summary>This union is used for conversation from 64-bits long integer random number
/// to double precision floating point number in interval (0, 1).</summary>
union GaUnsignedIntToDouble
{
/// <summary>This field is used to store 64-bit long integer number.</summary>
unsigned int bits[ 2 ];
/// <summary>This field is used to read 64-bit long integer number as mantissa of single precision floating point number.</summary>
double number;
};
/// <summary>Parameters used by <see cref="GaRandomGenerator" /> to generate random numbers.</summary>
enum GaRandomParams
{
GRP_BUFFER_SIZE = 17,
GRP_OFFSET = 10,
GRP_ROTATION_1 = 19,
GRP_ROTATION_2 = 27
};
/// <summary><c>GaRandomGenerator</c> class implements algorithm for generating 64-bits wide random unsigned integers and floating-point numbers.
/// It takes care of architecture's endianness, but underlying CPU architecture must support floating-point by IEEE 754 standard.
/// <c>GaRandomGenerator</c> class doesn't implement <see cref="GaRandom" /> interface. Primary purpose of this class is to provide service
/// for generating random numbers for classes which implement <see cref="GaRandom" /> interface.
///
/// This class has built-in synchronizator so it is allowed to use <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros with instances of this class.
/// All public methods are thread-safe.</summary>
class GaRandomGenerator
{
DEFINE_SYNC_CLASS
private:
/// <summary>Buffers used to generate bits of random numbers.</summary>
unsigned int _buffer[ 2 ][ GRP_BUFFER_SIZE ];
/// <summary>Stores current positions into buffer.</summary>
int _positions[ 2 ];
/// <summary>This attribute indicates endianness of architecture. If it is set to <c>true</c>, the architecture is little-endian,
/// if the architecture is big-endian this attribute is set to <c>false</c>.</summary>
bool _littleEndian;
public:
/// <summary>This constructor initialize random generator with current time as seed.</summary>
GaRandomGenerator() { Initalization( (unsigned int)time( NULL ) ); }
/// <summary>This constructor initialize random generator with user-defined seed.</summary>
/// <param name="seed">user-defined seed.</param>
GaRandomGenerator(unsigned long seed) { Initalization( seed ); }
/// <summary>This method generates 64-bit wide unsigned integer, and stores it in <c>word1</c> and <c>word2</c> parameters.
///
/// This method is thread-safe.</summary>
/// <param name="word1">reference to variable which is used to store 32 least significant bits of generated integer.</param>
/// <param name="word2">the reference to variable which is used to store 32 most significant bits of generated integer.</param>
GAL_API
void GACALL Generate(unsigned int& word1,
unsigned int& word2);
/// <summary><c>GeneratrFloat</c> method generates single precision floating point number i interval (0, 1).
///
/// This method is thread-safe.</summary>
/// <returns>Method returns generated number.</returns>
GAL_API
float GACALL GenerateFloat();
/// <summary><c>GeneratrFloat</c> method generates double precision floating point number i interval (0, 1).
///
/// This method is thread-safe.</summary>
/// <returns>Method returns generated number.</returns>
GAL_API
double GACALL GenerateDouble();
// Initialization of random generator
/// <summary>Initializes random generator with specified seed. <c>Initialization</c> method is called by constructor.
///
/// This method is not thread-safe.</summary>
/// <param name="seed">seed used to initialize generator.</param>
GAL_API
void GACALL Initalization(unsigned int seed);
};// END CLASS DEFINITION GaRandomGenerator
/// <summary>Interface for random value generators.</summary>
/// <param name="TYPE">type of generated values.</param>
template <typename TYPE>
class GaRandom
{
public:
/// <summary>This method generates random values of <c>TYPE</c> with no specific range.</summary>
/// <returns>Returns generate random value.</returns>
virtual TYPE GACALL Generate()=0;
/// <summary>This method generates random value of <c>TYPE</c> with specified maximum.</summary>
/// <param name="max">maximal value which can be generated.</param>
/// <returns>Returns generate random value.</returns>
virtual TYPE GACALL Generate(const TYPE& max)=0;
/// <summary>This method generates random value of <c>TYPE</c> within specified range of values.</summary>
/// <param name="min">minimal value which can be generated.</param>
/// <param name="max">maximal value which can be generated.</param>
/// <returns>Returns generate random value.</returns>
virtual TYPE GACALL Generate(const TYPE& min,
const TYPE& max)=0;
};// END CLASS DEFINITION GaRandom
/// <summary><c>GaRandomInteger</c> class generates random 32-bits wide integer numbers. The class implements <see cref="GaRandom" /> interface.
/// This class has no built-in synchronizator, so <c>LOCK_OBJECT</c> and <c>LOCK_THIS_OBJECT</c> macros cannot be used with instances of this class,
/// but all public methods are thread-safe.</summary>
class GaRandomInteger : public GaRandom<int>
{
private:
/// <summary>Instance of algorithm for generating random numbers.</summary>
GaRandomGenerator _generator;
public:
/// <summary>This constructor initializes random generator with current time as seed.</summary>
GaRandomInteger() { }
/// <summary>This constructor initialize random generator with user-defined seed.</summary>
/// <param name="seed">user-defined seed.</param>
GaRandomInteger(unsigned long seed) : _generator(seed) { }
/// <summary>This method generates random values in interval(0, 2147483647).
///
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -