Created
August 29, 2014 02:49
-
-
Save timmyshen/0c84a86821a8b025a809 to your computer and use it in GitHub Desktop.
random generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <chrono> | |
#include <random> | |
int main(int argc, char* argv []) | |
{ | |
auto x = 1; | |
std::cout << "auto variable x = " << x << std::endl; | |
srand(0u); | |
auto r = rand(); | |
std::cout << "rand() = " << r << std::endl; | |
std::cout << "next rand() = " << rand() << std::endl; | |
auto start = std::chrono::steady_clock::now(); | |
int sum(0); | |
for (size_t i = 0; i < 10000000; i++) | |
{ | |
sum += rand(); | |
} | |
auto end = std::chrono::steady_clock::now(); | |
auto span = end - start; | |
std::cout << "time span in seconds: " << span.count() << std::endl; | |
std::cout << "sum = " << sum << std::endl; | |
start = std::chrono::steady_clock::now(); | |
std::default_random_engine rand_eng; | |
std::normal_distribution<double> normal_std(0.0, 1.0); | |
auto rd = normal_std(rand_eng); | |
std::cout << "random double = " << rd << std::endl; | |
double dsum(0.0); | |
for (size_t i = 0; i < 10000000; i++) | |
{ | |
dsum += normal_std(rand_eng); | |
} | |
end = std::chrono::steady_clock::now(); | |
span = end - start; | |
std::cout << "time span in seconds: " << span.count() << std::endl; | |
std::cout << "dsum = " << dsum << std::endl; | |
start = std::chrono::steady_clock::now(); | |
std::default_random_engine rand_eng1; | |
double dsum2(0.0); | |
for (size_t i = 0; i < 10000000; i++) | |
{ | |
std::normal_distribution<double> normal_std(0.0, 1.0); | |
dsum2 += normal_std(rand_eng1); | |
} | |
end = std::chrono::steady_clock::now(); | |
span = end - start; | |
std::cout << "time span in seconds: " << span.count() << std::endl; | |
std::cout << "dsum2 = " << dsum2 << std::endl; | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment