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;
}

發佈了60 篇原創文章 · 獲贊 15 · 訪問量 66萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章