笔记1 - 随机种子+类型萃取

1.随机数

      均匀分布 ,并计算耗时。

#include <vector>
#include <random>

static int get_random(int min_value, int max_value)
{
	static std::random_device rd;		//生成器
	using dist_ = std::uniform_int_distribution<int>;		//分布器	(均匀分布 uniform, 正态分布 normal, 二项分布 binomial, 泊松分布poisson)
	static dist_ funcDist/* {}*/;
	return funcDist(rd, dist_::param_type{ min_value, max_value });
}
static void test_random_2()
{
	auto t_begin = chrono::steady_clock::now();
	for (int i = 0; i < TOTAL; i++) {
		if (i % 200 == 0) {
			printf("\n%3d: ", i / 100);
		}
		//printf("%03d ", test_random_2(1, 999));
		get_random(1, 999);
	}
	auto t_end = chrono::steady_clock::now();

	chrono::duration<double, std::ratio<1, 1000>> elap_msec = t_end - t_begin;

	std::cout << "\n" << elap_msec.count() << "\n";
}

    正态分布的例子

static void test_random_normal()		
{
	static default_random_engine re(time(NULL));   //默认引擎

	static normal_distribution<double> nd(31 /* mean */, 8 /* sigma */);

	auto norm = std::bind(nd, re);

	vector<int> mn(64);
	for (int i = 0; i < 1200; ++i)
		++mn[round(norm())]; // 产生随机数

	for (int i = 0; i < mn.size(); ++i)	{
		cout << i << '\t';
		for (int j = 0; j < mn[i]; ++j)
			cout << '*';

		cout << '\n';
	}
}

2.类型萃取

    1.   类型萃取、类型判断和随机分配的例子

    for (int i = 0; i < 81; i++) {
        m_gridPos[i] = i;
    }
   
    int kk = sizeof(decltype(m_gridPos+0) );        //64位的值为8, 32位为4
    int kkk = sizeof(decltype(m_gridPos[0]));        //64位、32位都是int的大小,4字节

    using T0 = std::decay_t<decltype(m_gridPos + 0)>;       //int*
    using T1 = std::decay_t<decltype(m_gridPos[0])>;        //int
    using T2 = std::decay_t<decltype(*m_gridPos)>;          //int
    using T = decltype(m_gridPos[0]);
    if constexpr (std::is_same_v<T1, int>) {
        std::cout << "ok. it's same\n";        //输出(类型相同)
    }
    else {
        std::cout << "error!!!\n";
    }
    if constexpr (!std::is_same_v<T, int>) {
        std::cout << "ok. it is not same!\n";    //输出(类型不同)
    }
    else {
        std::cout << "error!!!\n";
    }


    random_shuffle(m_gridPos, m_gridPos + sizeof(m_gridPos) / sizeof(decltype(m_gridPos[0])));
    random_shuffle(m_gridPos, m_gridPos + 81, genRandNum);

   2.std::visit 遍历 std::variant 对象中包含的所有对象

void test()
 {
     	std::variant<int, double, std::string> v{ 100.3 };
		std::visit([](auto&& arg) {
			using T = std::decay_t<decltype(arg)>;
			if constexpr (std::is_same_v<T, int>)
			{
				std::cout << "arg is integer, value:" << arg << '\n';
			}
			else if constexpr (std::is_same_v<T, float>) {
				std::cout << "arg is float, value: " << arg << '\n';
			}
			else if constexpr (std::is_same_v<T, double>) {
				std::cout << "arg is double, value:" << arg << "\n";
			}
			else if constexpr (std::is_same_v<T, std::string>)
			{
				std::cout << "arg is string, value: " << arg << '\n';
			}
			else {
				std::cout << "arg is unknown!" << arg << "\n";
			}
		},
		v);

 }

  3.std::copy,一般而言,配合std::ostream_iterator使用比for循环输出效率更高

/*{
		std::vector<int> vecDest(5);
		std::vector<int> vec = { 1,2,5,10,20 };
		std::copy(vec.begin(), vec.end(), &vecDest.front());
		std::copy(vecDest.begin(), vecDest.end(), std::ostream_iterator<int>(std::cout, ", "));
}

{
		vector<int> v(10);
		std::iota(v.begin(), v.end(), 0);  //generating a value from 0-9

		cout << "before:";
		copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
		cout << endl;
}
*/

	auto seed = chrono::system_clock::now().time_since_epoch().count();

	{
		std::vector<int> l(13);
		std::iota(l.begin(), l.end(), 9);		//初始化值,从9开始依次递增。
		std::shuffle(l.begin(), l.end(), std::default_random_engine(seed));		//c++17  random_shuffle -> shuffle
		std::cout << "shuffle: ";
		std::copy(l.begin(), l.end(), std::ostream_iterator<int>(std::cout, ", "));

		{
			std::vector<int> tmp(l.size());
			std::copy(l.begin(), l.end(), tmp.begin());		//必须保证目标容器的大小不小于源容器大小
			std::cout << endl << "shuffle&copy: ";
			std::copy(tmp.begin(), tmp.end(), std::ostream_iterator<int>(std::cout, ", "));
		}
		std::cout << endl;
	}

  注意:std::copy,必须预先设定好目标容器的大小,不能小于源容器大小

4. 获取一个随机数

int get_random(int iMin, int iMax)

{

        auto begin = chrono::system_clock::now();

        auto seed = std::chrono::system_clock::now().time_since_epoch().count();

        std::default_random_engine re(seed);

        std::uniform_int_distribute<int> i_dist(iMin, iMax);

        std::this_thread::sleep_for(chrono::nanoseconds(10));

        auto end = chrono::system_clock::now();

        std::duration<double, std::ratio<1, 1000>> _d(end-begin);

        int time_elapse = _d.count();

        int ret = i_dist(re);                        //产生随机数,用于返回。

}

int random = get_random(1, 100);        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值