性能较好的随机数发生器
来源:互联网 发布:同花顺交易软件下载 编辑:程序博客网 时间:2024/05/05 17:45
产生随机数的最简单的方法是线性同余数发生器,它于1951年由Lehmer首先提出,数x1,x2,... 的生成满足:
xi+1 = Axi mod M
为了开始这个序列,必须给出x0的某个值,这个值叫做种子(seed)。
static const int A = 48271;
static const int M = 2147483647;
static const int Q = M / A;
static const int R = M % A;
class Random
...{
public:
explicit Random( int initialValue = 1 );
int randomInt();
double random0_1();
int randomInt( int low, int high);
private:
int state;
};
/**//**
* Construct with initialValue for the state.
*/
Random::Random( int initialValue)
...{
if( initialValue < 0)
initialValue += M;
state = initialValue;
if( state == 0)
state = 1;
}
/**//**
*Return a pseudorandom int, and change the internal state.
*/
int Random::randomInt()
...{
int tmpState = A * ( state % Q ) - R * (state / Q );
if( tmpState >= 0)
state = tmpState;
else
state = tmpState + M;
return state;
}
/**//**
* Return a pseudorandom double in the open range 0..1
* and change the internal state.
*/
double Random::random0_1( )
...{
return (double) randomInt( ) / M;
}
/**//**
* Return an int in the closed range [low,high], and
* change the internal state.
*/
int Random::randomInt( int low, int high )
...{
double partitionSize = (double) M / ( high - low + 1 );
return (int) ( randomInt( ) / partitionSize ) + low;
}
static const int M = 2147483647;
static const int Q = M / A;
static const int R = M % A;
class Random
...{
public:
explicit Random( int initialValue = 1 );
int randomInt();
double random0_1();
int randomInt( int low, int high);
private:
int state;
};
/**//**
* Construct with initialValue for the state.
*/
Random::Random( int initialValue)
...{
if( initialValue < 0)
initialValue += M;
state = initialValue;
if( state == 0)
state = 1;
}
/**//**
*Return a pseudorandom int, and change the internal state.
*/
int Random::randomInt()
...{
int tmpState = A * ( state % Q ) - R * (state / Q );
if( tmpState >= 0)
state = tmpState;
else
state = tmpState + M;
return state;
}
/**//**
* Return a pseudorandom double in the open range 0..1
* and change the internal state.
*/
double Random::random0_1( )
...{
return (double) randomInt( ) / M;
}
/**//**
* Return an int in the closed range [low,high], and
* change the internal state.
*/
int Random::randomInt( int low, int high )
...{
double partitionSize = (double) M / ( high - low + 1 );
return (int) ( randomInt( ) / partitionSize ) + low;
}
- 性能较好的随机数发生器
- 正态分布的随机数发生器
- OpenCV的随机数发生器
- 密码学里的随机数发生器
- 密码学里的随机数发生器
- 随机数发生器
- 随机数发生器
- 随机数发生器
- 随机数发生器
- 随机数发生器
- 随机数发生器
- 随机数发生器
- 随机数发生器
- 随机数发生器
- 正态分布的随机数发生器 in C#
- 正态分布的随机数发生器 in C#
- 正态分布的随机数发生器 in C#
- 密码学里的随机数发生器[详细]
- IBM 软件集团的收购清单
- SQL Server 安装问题:以前的某个程序安装已在安装计算机上创建挂起的文件操作
- Rome was not be built in a day
- POST与GET的区别
- 关键字:mutable
- 性能较好的随机数发生器
- SQL SERVER 2005使用入门
- Lucene学习笔记(准备)
- Matlab2008a:惊喜
- 深度精简系统的IIS安装
- 基于pwlib(ptlib)连接MySql
- 贫
- .NET环境下水晶报表使用总结
- sql2000和sql2005装在同一台机器上是需要加实例名