OpenCV之随机类RNG

/*!
   Random Number Generator

   The class implements RNG using Multiply-with-Carry algorithm
*/
class CV_EXPORTS RNG
{
public:
    enum { UNIFORM=0, NORMAL=1 };

	
RNG();//默认构造函数
// inline RNG::RNG() { state = 0xffffffff; }

RNG(uint64 state);//带参数的构造函数,接受一个64位无符号的值。
//inline RNG::RNG(uint64 _state) { state = _state ? _state : 0xffffffff; }


//! updates the state and returns the next 32-bit unsigned integer random number
    unsigned next();
/*
inline unsigned RNG::next()
{
    state = (uint64)(unsigned)state*CV_RNG_COEFF + (unsigned)(state >> 32);
    return (unsigned)state;
}
#define CV_RNG_COEFF 4164903690U
用两个很大的无符号数相乘,乘积结果要转换为64位无符号数,转换的时候两个乘数应该向高精度看起,所以应该也先转换为64位再相乘。把state右移32位得到一个数,把这两个数相加。函数返回一个32位的无符号数,其值为截断前面求得的和。
*/


//以下几个函数是从类到uchar.schar,ushort,short,usinged的显示转换函数
operator uchar();//返回一个8位无符号类型的随机数,把next返回的数截断
//inline RNG::operator uchar() { return (uchar)next(); }


operator schar();//返回一个8为有符号类型的随机数。???会产生负数吗,返回的也是截断的next返回值。莫非是截断后得到的最高位作为符号位,这样也可能是随机的。???
//inline RNG::operator schar() { return (schar)next(); }


operator ushort();//返回一个无符号16为整数
//inline RNG::operator ushort() { return (ushort)next(); }

operator short();//返回一个有符号16为整数
// inline RNG::operator short() { return (short)next(); }


operator unsigned();//返回一个无符号32为整数
// inline RNG::operator unsigned() { return next(); }


//! returns a random integer sampled uniformly from [0, N).
unsigned operator ()(unsigned N);//重载括号操作符,带参数。在(0,N)之间返回一个整数,调用uniform成员函数
//inline unsigned RNG::operator ()(unsigned N) {return (unsigned)uniform(0,N);}


unsigned operator ()();//重载括号操作符,无参数。直接返回next结果。
// inline unsigned RNG::operator ()() {return next();}


//放在这个位置有点奇怪,为什么不和前边同类放一起呢?放回一个带符//号32为整数
operator int();
// inline RNG::operator int() { return (int)next(); }

//返回一个float型(具体多少位看平台)数。
operator float();
// inline RNG::operator float() { return next()*2.3283064365386962890625e-10f; }

//两个数按位或一下,解释起来好麻烦
operator double();
/*
inline RNG::operator double()
{
    unsigned t = next();
    return (((uint64)t << 32) | next())*5.4210108624275221700372640043497e-20;
}*/


//! returns uniformly distributed integer random number from [a,b) range
int uniform(int a, int b);//[a,b)内随机产生一个int型值,均匀的哦!
// inline int RNG::uniform(int a, int b) { return a == b ? a : (int)(next()%(b - a) + a); }


//! returns uniformly distributed floating-point random number from [a,b) range
float uniform(float a, float b); //[a,b)内随机产生一个float型值,均匀的哦!
// inline float RNG::uniform(float a, float b) { return ((float)*this)*(b - a) + a; }


//! returns uniformly distributed double-precision floating-point random number from [a,b) range
double uniform(double a, double b); //[a,b)内随机产生一个double型值,均匀的
// inline double RNG::uniform(double a, double b) { return ((double)*this)*(b - a) + a; }

void fill( InputOutputArray mat, int distType, InputArray a, InputArray b, bool saturateRange=false );//这个函数实现很长,暂时略过。

//! returns Gaussian random variate with mean zero.
double gaussian(double sigma);//返回均值为0的高斯随机变量,
/*double RNG::gaussian(double sigma)
{
    float temp;
    randn_0_1_32f( &temp, 1, &state );
    return temp*sigma;
}*/
	


uint64 state;//种子,next中需要这样一个初始值
};

你可能感兴趣的:(OpenCV之随机类RNG)