C++随机数生成器及分布器相关

导读:本篇文章讲解 C++随机数生成器及分布器相关,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

C++11版本:
随机数由生成器和分布器结合产生。
https://www.cnblogs.com/DswCnblog/p/5624869.html
http://c.biancheng.net/view/638.html

随机数生成器:
https://blog.csdn.net/u014787464/article/details/50736079
https://blog.csdn.net/qq_22642239/article/details/103031758?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
std::default_random_engine

默认的随机数生成器,每次产生的随机数序列是固定的,方便调试,需要产生不同的随机数序列,需要指定随机数种子,其实就是给一个每次执行程序时产生不同结果的一个数。
一般可以用时间作为种子

均匀分布:
http://c.biancheng.net/view/639.html

代码测试:

/*
 * @Author: Haiming Zhang
 * @Email: zhanghm_1995@qq.com
 * @Date: 2020-04-10 22:29:01
 * @LastEditTime: 2020-04-12 21:10:59
 * @References: 
 * @Description: Learn the random number generate related process
 */

#include <iostream>
#include <random>
#include <ctime>
#include <map>
#include <iomanip>

using std::cout;
using std::endl;

void TestRandomGenerator() {
    std::random_device rd;
    std::default_random_engine rngl(rd());
    std::uniform_int_distribution<int> UniDist(1, 10);
    for (int i = 0; i < 20; ++i) {
        cout<<UniDist(rngl)<<endl;;
    }

    cout<<"==============="<<endl;
    for (int n = 0; n < 20; ++n) {
        cout<<rngl()<<endl;
    }
}

void NormalDistribution() {
    std::random_device rd;
    std::mt19937 gen(rd());
 
    // values near the mean are the most likely
    // standard deviation affects the dispersion of generated values from the mean
    // std::normal_distribution<> d(0,2);

    std::uniform_int_distribution<> d(1,10);
 
    std::map<int, int> hist;
    for(int n=0; n<10000; ++n) {
        ++hist[std::round(d(gen))];
    }
    for(auto p : hist) {
        std::cout << std::fixed << std::setprecision(1) << std::setw(2)
                  << p.first << ' ' << std::string(p.second/200, '*') << '\n';
    }
}

void TestDistribution() {
    std::random_device rd;  // 将用于为随机数引擎获得种子
    std::mt19937 gen(rd()); // 以播种标准 mersenne_twister_engine

    std::uniform_int_distribution<int> UniDist(1, 10);
    for (int i = 0; i < 20; ++i) {
        cout<<UniDist(gen)<<endl;;
    }

    std::normal_distribution<float> NormDist(1, 30);
    for (int i = 0; i < 50; ++i) {
        cout<<NormDist(gen)<<endl;;
    }
}

/**
 * @brief 产生指定int范围内的随机数,不包括b
 */ 
void GenerateIntRand(int a, int b) {
    const int max_temp = b - a;
    
    /// 产生int类型随机数
    for (int i = 0; i < 50; ++i) {
        auto r = (std::rand() % max_temp) + a;
        cout<<r<<endl;
    }
}

int main() {
    // GenerateIntRand(8, 20);

    // TestDistribution();

    // TestRandomGenerator();

    NormalDistribution();

    return 0;
}
std::vector<std::shared_ptr<GRANSAC::AbstractParameter>> GenerateCandidatePoints2(cv::Mat& image, const int max, const int nPoints)
{
    auto Line = [](int x) -> int {
        return static_cast<int>(550 - 0.5 * x);
    };

	std::random_device SeedDevice;
	std::mt19937 RNG = std::mt19937(SeedDevice());

	std::uniform_int_distribution<int> UniDist(0, max - 1); // [Incl, Incl]
	int Perturb = 25;
	std::normal_distribution<GRANSAC::VPFloat> PerturbDist(0, Perturb);

	std::vector<std::shared_ptr<GRANSAC::AbstractParameter>> CandPoints;
	for (int i = 0; i < nPoints; ++i)
	{
		int Diag = UniDist(RNG);
		// cv::Point Pt(floor(Diag + PerturbDist(RNG)), floor(Diag + PerturbDist(RNG)));
		cv::Point Pt(floor(Diag), floor(Line(Diag) + PerturbDist(RNG)));

		if (i % 10 == 0) {
			Pt.y += 150;
		}
		cv::circle(image, Pt, floor(max / 100) + 3, cv::Scalar(0, 0, 0), 2, cv::LINE_AA);
		

		std::shared_ptr<GRANSAC::AbstractParameter> CandPt = std::make_shared<Point2D>(Pt.x, Pt.y);
		CandPoints.push_back(CandPt);
	}

    cv::line(image, cv::Point(0, Line(0)), cv::Point(max - 1, Line(max - 1)), cv::Scalar(255, 0, 0), 3, cv::LINE_AA);
    return CandPoints;
}

int GenerateIntRand(int a, int b) {
    const int max_temp = b - a;
    
    auto r = (std::rand() % max_temp) + a;
	return r;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/121218.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!