随机生成元素升序向量_Ransac算法所用:C++利用rand()生成不重复的随机数以及vector数组随机重排序 以及 rand()的应用…

在哪个位置设置随机数生成种子,利用什么思想么设置哪个头文件中/p>

怎么用rand()生成0-i区间的随机数用了数学中的哪种运算/p>

随机向量的生成问题/p>

应用1

生成0~i区间内的随机数:

#include

using namespace std;

rand() % (i + 1);

rand() % (i + 1) + begin; //生成begin到i之间的随机数

if(rand() % 2) // rand()%2为0或1各有50%的概率。

在软件测试中,随机向量的生成非常基本的操作

置乱算法permute(),封装到向量Vector::unsort()置乱操作接口中。

应用2

首先要在生成随机数函数的前面设置一个种子,利用srand()函数,在头文件cstdlib中:

新建一个随机数生成类可以在类的构造函数中设置种子,增加代码的重用。

#ifndef __RANDOMNUMBER_H__

#pragma once

#define __RANDOMNUMBER_H__

#include //time

#include //rand srand

#include “opencv2/opencv.hpp”

using namespace std;

using namespace cv;

class RandomNumber {

public:

RandomNumber() {

srand(time(0));

}

int GetRandomNum(int begin = 0, int end = 1) {

return rand() % (end – begin + 1) + begin;

}

vector GetRandomVector(vector contours)

{

random_shuffle(contours.begin(), contours.end());

return contours;

}

};

#endif

可以实现生成某范围的随机数和vector数组元素随机重排排序。

用法:

//加入一些噪声点

Point2f noisePoints;

RandomNumber r, s;//提前设置种子

for (int i = 0; i < 48; i++)

{

noisePoints.x = corMidContours[i].x + r.GetRandomNum(-20, 20);

noisePoints.y = corMidContours[i].y + s.GetRandomNum(-20, 20);

corMidContours.push_back(noisePoints);

}

RandomNumber r;//用类的方法设定种子

while (k < kMax)

{

if (!RandomSelectPoints(r, corMidContours, nLeast, maybeinliers, testData))//随机获取其他样本点

return;

}

RandomSelectPoints函数如下:

bool CEllipse::RandomSelectPoints(RandomNumber r, vector contours,int nNums, vector& outContours, vector& outTestContours)

{

if (outContours.size() != 0)

outContours.clear();

if (outTestContours.size() != 0)

outTestContours.clear();

//random_shuffle(contours.begin(), contours.end());

contours = r.GetRandomVector(contours);//调用vector随机不重复重排序函数

if (nNums > contours.size())

return false;

for (int i = 0; i < nNums; i++)

{

outContours.push_back(contours[i]);

}

for (int i = nNums; i < contours.size(); i++)

{

outTestContours.push_back(contours[i]);

}

}

文章知识点与官方知识档案匹配,可进一步学习相关知识算法技能树首页概览33914 人正在系统学习中 相关资源:丝柏人像美肤处理软件CPAC Imaging Pro 3绿化汉化破解版

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年11月25日
下一篇 2020年11月25日

相关推荐