现在的位置: 首页 > 综合 > 正文

Opencv中RNG

2013年09月19日 ⁄ 综合 ⁄ 共 1298字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.