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

📄 random.cpp

📁 遗传算法做图像的模式匹配
💻 CPP
字号:

/*! \file Random.cpp
    \brief This file contains implementation 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.
 *
 */

#include "Random.h"

#if defined (GAL_SYNTAX_GNUC) || defined (GAL_SYNTAX_SUNC)

inline unsigned int _lrotl(unsigned int x, int r)
{
	return ( x<< r ) | ( x >> ( sizeof( x ) * 8 - 1 ) );
}

#endif

namespace Common
{

	// Generator of random bits
	void GaRandomGenerator::Generate(unsigned int& word1,
		unsigned int& word2)
	{
		LOCK_THIS_OBJECT( lock );

		// generate next number
		word1 = _lrotl( _buffer[ 1 ][ _positions[ 0 ] ], GRP_ROTATION_2 ) + _buffer[ 1 ][ _positions[ 1 ] ];
		word2 = _lrotl( _buffer[ 0 ][ _positions[ 0 ] ], GRP_ROTATION_1 ) + _buffer[ 0 ][ _positions[ 1 ] ];

		// save to buffer
		_buffer[ 0 ][ _positions[ 0 ] ] = word1;
		_buffer[ 1 ][ _positions[ 0 ] ] = word2;

		// next buffer position
		_positions[ 0 ] = ( _positions[ 0 ] + 1 ) % GRP_BUFFER_SIZE;
		_positions[ 1 ] = ( _positions[ 1 ] + 1 ) % GRP_BUFFER_SIZE;
	}

	// Generate random single precision floating point number in interval 0..1
	float GaRandomGenerator::GenerateFloat()
	{
		GaUnsignedIntToFloat converter;

		// generate random bits
		unsigned int dummy;
		Generate( converter.bits, dummy );

		// covert to double
		converter.bits = ( converter.bits & 0x007FFFFF ) | 0x3F800000;

		return converter.number - 1;
	}

	// Generate random double precision floating point number in interval 0..1
	double GaRandomGenerator::GenerateDouble()
	{
		GaUnsignedIntToDouble converter;

		// generate random bits
		Generate( converter.bits[ 0 ], converter.bits[ 1 ] );

		// covert to double
		converter.bits[ _littleEndian ] = ( converter.bits[ 0 ] & 0x000FFFFF ) | 0x3FF00000;

		return converter.number - 1;
	}

	// Initialization of random generator
	void GaRandomGenerator::Initalization(unsigned int seed)
	{
		// make random numbers and put them into the buffer
		for( int i = 0; i < GRP_BUFFER_SIZE; i++ )
		{
			for( int j = 0; j < 2 ; j++ )
			{
				seed *= 0xD3A9B4FA;
				_buffer[ j ][ i ] = ++seed;
			}
		}

		_positions[ 0 ] = GRP_OFFSET;
		_positions[ 1 ] = 0;

		// detecting big or little endian
		GaUnsignedIntToDouble converter;
		converter.number = 1;
		_littleEndian = converter.bits[ 1 ] == 0x3FF00000;
	}

} // Common

⌨️ 快捷键说明

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