用OpenCV做算法的朋友們肯定爲隨機數煩惱過,新版本一直支持隨機數產生器啦,而且還繼續支持之前版本的c格式的函數,不過與時俱進,我這裏介紹C++的RNG類。它可以壓縮一個64位的i整數並可以得到scalar和array的隨機數。目前的版本支持均勻分佈隨機數和Gaussian分佈隨機數。隨機數的產生採用的是Multiply-With-Carry算法和Ziggurat算法。
其構造函數的初始化可以傳入一個64位的整型參數作爲隨機數產生器的初值。next可以取出下一個隨機數,uniform函數可以返回指定範圍的隨機數,gaussian函數返回一個高斯隨機數,fill則用隨機數填充矩陣。
這裏介紹一個uniform的使用事項,就是比如利用它產生0~1的隨機數的問題,具體代碼如下:
- RNG rng;
- // always produces 0
- double a = rng.uniform(0, 1);
- // produces double from [0, 1)
- double a1 = rng.uniform((double)0, (double)1);
- // produces float from [0, 1)
- double b = rng.uniform(0.f, 1.f);
- // produces double from [0, 1)
- double c = rng.uniform(0., 1.);
- // may cause compiler error because of ambiguity:
- // RNG::uniform(0, (int)0.999999)? or RNG::uniform((double)0, 0.99999)?
- 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