天天看点

opencv随机数发生器RNG

用OpenCV做算法的朋友们肯定为随机数烦恼过,新版本一直支持随机数产生器啦,而且还继续支持之前版本的c格式的函数,不过与时俱进,我这里介绍C++的RNG类。它可以压缩一个64位的i整数并可以得到scalar和array的随机数。目前的版本支持均匀分布随机数和Gaussian分布随机数。随机数的产生采用的是Multiply-With-Carry算法和Ziggurat算法。

其构造函数的初始化可以传入一个64位的整型参数作为随机数产生器的初值。next可以取出下一个随机数,uniform函数可以返回指定范围的随机数,gaussian函数返回一个高斯随机数,fill则用随机数填充矩阵。

这里介绍一个uniform的使用事项,就是比如利用它产生0~1的随机数的问题,具体代码如下:

[cpp]  view plain  copy

  1. RNG rng;  
  2. // always produces 0  
  3. double a = rng.uniform(0, 1);  
  4. // produces double from [0, 1)  
  5. double a1 = rng.uniform((double)0, (double)1);  
  6. // produces float from [0, 1)  
  7. double b = rng.uniform(0.f, 1.f);  
  8. // produces double from [0, 1)  
  9. double c = rng.uniform(0., 1.);  
  10. // may cause compiler error because of ambiguity:  
  11. // RNG::uniform(0, (int)0.999999)? or RNG::uniform((double)0, 0.99999)?  
  12. double d = rng.uniform(0, 0.999999);  

就是不能写成rng.uniform( 0 , 1),因为输入为int型参数,会调用uniform(int,int),只能产生0。请大家注意使用^_^

还有一些随机数相关的函数,比如randu可以产生一个均匀分布的随机数或者矩阵,randn可以产生一个正态分布的随机数,randShuffle可以随机打乱矩阵元素

下面是类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中需要这样一个初始值  
};  
           

再简单介绍一下c版本的随机数产生器的相关函数,有cvRNG、cvRandArr、cvRandInt、cvRandReal

参考博文:随机数产生器RNG   随机类RNG

继续阅读