代碼的高效實現

1. C++:rand()隨機數和mt19937隨機數

C++的 rand() 函數產生的隨機數範圍值是 0~32767 ,如果想產生很大的隨機數(幾億,甚至幾十億),就要用到 mt19937 。

  • rand(); //0~32767 取範圍[n,m]的數,寫法爲 rand()%(m-n+1)+n
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
	srand(unsigned(time(0)));
	int count = rand() % 3 + 1;    //範圍1~3
	int count1 = rand() % 3;    //範圍0~2
	cout << count << endl << count1 << endl;
	return 0;
}
  • mt19937
#include <iostream>
#include <chrono>
#include <random>
using namespace std;
int main()
{
    // 隨機數種子
	unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    mt19937 rand_num(seed);	 // 大隨機數,無範圍
	cout << rand_num() << endl;
	uniform_int_distribution<long long> dist(0, 1000000000);  // 給定範圍
	cout << dist(rand_num) << endl;
	return 0;
}

2、Eigen:SSE加速,*** READ THIS WEB PAGE !!! ****"’ failed.

參考連接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章