📄 vmcryptorandomnumber.cpp
字号:
/*****************************************************************************/
/* SOURCE FILE */
/*****************************************************************************/
/*
$Archive: $
$Revision: $
$Date: $
$Author: $
Description: Implementation of a class that creates random numbers.
Based on work put into the public domain by William S. England
(Oct 1988) based on an article by:
Stephen K. Park and Keith W. Miller.
RANDOM NUMBER GENERATORS: GOOD ONES ARE HARD TO FIND.
Communications of the ACM, New York, NY.,October 1988 p.1192
The modulus and multiplier have been extensively tested and
should not be changed except by someone who is a professional
Lehmer generator writer.
THIS GENERATOR REPRESENTS THE MINIMUM STANDARD AGAINST WHICH
OTHER GENERATORS SHOULD BE JUDGED.
("Quote from the referenced article's authors. WSE" )
*/
static char OBJECT_ID[] = "$Revision: 1 $ : $Date: 2/04/99 10:16a $";
/*****************************************************************************/
#include "VMCryptoRandomNumber.h"
/*****************************************************************************
The values for culq and ciur are pre-calculated below to compensate
for compilers that may overflow when building the code.
culq = ( culm / cuia )
cuir = ( culm % cuia )
******************************************************************************/
const unsigned long culm = 2147483647;
const unsigned int cuia = 16807;
const unsigned long culq = 127773;
const unsigned int cuir = 2836;
const unsigned long cultest = 1043618065;
/*****************************************************************************
F(z) = ( cuia * z ) % culm
= ( cuia * z ) - ( culm * ( az / culm ) )
F(z) = G(z) + ( culm * T(z) )
G(z) = ( cuia * ( z % culq ) )- ( cuir * ( z / culq ) )
T(z) = ( z / culq ) - ( ( cuia * z ) / culm )
F(z) = ( cuia * ( z % culq ) ) - (( cuir * z ) / culq )
+ ( culm * ( ( z / culq ) - ( cuia * ( z / culm ) ) ) )
F(z) = ( cuia * ( z % culq ) ) - ( ( cuir * z )/ culq ) + ( culm * ( z / culq ) ) - ( cuia * z )
******************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMCryptoRandomNumber::SetSeed
DESCRIPTION: seed the class
INPUT: ulSeed - the new seed value
OUTPUT: none
RETURNS: void
*/
void VMCryptoRandomNumber::SetSeed( unsigned long ulSeed )
{
m_ulSeed = ulSeed;
}
/* End of function "VMCryptoRandomNumber::SetSeed"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMCryptoRandomNumber::GetRandomNumber
DESCRIPTION: get the next one
INPUT: void
OUTPUT: none
RETURNS: unsigned long
*/
unsigned long VMCryptoRandomNumber::GetRandomNumber( void )
{
int iLow;
int iHigh;
int iTest;
iHigh = m_ulSeed / culq;
iLow = m_ulSeed % culq;
iTest = ( cuia * iLow ) - ( cuir * iHigh );
if ( iTest > 0 )
m_ulSeed = iTest;
else
m_ulSeed = iTest + culm;
return m_ulSeed;
}
/* End of function "VMCryptoRandomNumber::GetRandomNumber"
/*****************************************************************************/
/*****************************************************************************/
/* Check-in history */
/*
*$Log: $
*/
/*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -