Opencv中RNG
Opencv中的RNG類主要用來生成隨機數,此類的定義如下:
class CV_EXPORTS RNG
{
public:
enum { UNIFORM=0, NORMAL=1 };
RNG();
RNG(uint64 _state);
//! updates the state and returns the next 32-bit unsigned integer random number
unsigned next();
operator uchar();
operator schar();
operator ushort();
operator short();
operator unsigned();
//! returns a random integer sampled uniformly from [0, N).
unsigned operator()(unsigned N);
unsigned operator ()();
operator int();
operator float();
operator double();
//! returns uniformly distributed integer random number from [a,b) range
int uniform(int a, int b);
//! returns uniformly distributed floating-point random number from [a,b) range
float uniform(float a, float b);
//! returns uniformly distributed double-precision floating-point random number from [a,b) range
double uniform(double a, double b);
void fill( InputOutputArray mat, int distType, InputArray a, InputArray b );
//! returns Gaussian random variate with mean zero.
double gaussian(double sigma);
uint64 state;
};
此類的定義中涉及到許多的運算子過載,在函式呼叫時應該予以注意。
double uniform(double a, double b)函式主要用來生成一個[a,b)範圍內的一個隨機數,主要通過如下實現
inline double RNG::uniform(double a, double b) { return ((double)*this)*(b - a) + a; }
這裡的double用到了操作符的過載,*this主要作為一個右運算元,相等於函式的引數,過載操作的實現主要通過以下實現:
inline RNG::operator double()
{
unsigned t = next();
return (((uint64)t << 32) | next())*5.4210108624275221700372640043497e-20;
}