在很多过程中,我们需要生成一些随机数,opencv中生成随机数的方法如下:
测试程序如下:【每次生成20个(0,100)之间的随机数,共生成10次】
#include<iostream>
#include<time.h>
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
RNG rng(12345);
//RNG rng;
//RNG rng((unsigned)time(NULL));
int main(void)
{
for (int i = 0; i < 10; i++)
{
cout <<"i = "<<i<< ": ";
for (int j = 0; j < 20; j++)
{
int a = rng.uniform(1, 100);
cout << a << ",";
}
cout << endl;
}
return 0;
}
(1)RNG rng(12345);
运行两次的结果一样,都是:
(2)RNG rng;
运行两次的结果一样,都是:
(3)RNG rng((unsigned)time(NULL));
每次运行的结果不一样:
注意:这种方法的随机性涉及时间间隔,故比较慢。
说明:每次产生的10个随机数中,可能有重复的值。