一、初始化
theRNG()
返回的类型是RNG&
RNG& rng=theRNG();
二、得到随机数
1.theRNG()
unsigned
是为了得到自然数,%
是为了选择特定范围的数
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
//初始化rng
RNG& rng=theRNG();
int a=(unsigned)rng;
cout<<a<<endl;
//130063606
for(int i=0;i<3;i++)
{
int b=(unsigned)rng%100;
cout<<b<<endl;
}
/*
97
39
29
/*
return 0;
}
2.均匀分布rng.uniform()
原型
int uniform (int a, int b)
float uniform (float a, float b)
double uniform (double a, double b)
返回值
返回[a,b)范围内均匀分布的整数随机数。
例
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
//初始化rng
RNG& rng=theRNG();
for(int i=0;i<3;i++)
{
int b=rng.uniform(0,100);
cout<<b<<endl;
}
/*
6
97
39
*/
return 0;
}
例:四分之一机会
// theRNG().uniform(0,4)返回[0,4)范围内均匀分布的整数随机数,即四分之一机会返回0
if(theRNG().uniform(0,4) != 0){
...
}
3.正态分布cv::randn()
原型:
void cv::randn (
InputOutputArray dst,
InputArray mean,
InputArray stddev
)
brief
randn用指定的平均向量和标准差矩阵将正态分布随机数填充矩阵dst。
生成的随机数被剪裁以适合输出数组数据类型的值范围。
Parameters
dst
:随机数的输出数组;数组必须预先分配,并且有1到4个通道。mean
:生成的随机数的平均值(期望值)。stddev
:生成的随机数的标准差;它可以是向量(对角标准差矩阵)或平方矩阵。
例
Mat state(2, 1, CV_32F);
randn( state, Scalar::all(0), Scalar::all(0.1) );
/**
[-0.13063084;
0.082345009]
*/