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

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值