string - C++ Random number generator apparent malfunction: am I unlucky? -
i wanted try c++ random number generator couldn't seem obtain satisfying results. example here attempt among others create random string of characters.
#include <iostream> #include <string> #include <random> std::string f(unsigned int n){ std::uniform_int_distribution<int> dis025(0, 25); std::mt19937 gen_mt(n); std::string str(5, '\0'); for(int = 0; i<5; i++) str[i] = (char)('a' + dis025(gen_mt)); return str; } int g(unsigned int n, int m){ std::uniform_int_distribution<int> dis(0, m); std::mt19937 gen_mt(n); return dis(gen_mt); } int main() { std::string s = f(g(106175305, 40000000)) + " " + f(g(53718209, 40000000)); std::cout << "random string: " << s << std::endl; }
(i had use f(g())
trick stops shouting insults.)
quite annoying , doubt desired behavior. somehow helpless prevent it, keeps happening, again...
#include <iostream> #include <string> #include <random> std::string fx(unsigned int n, int m){ std::uniform_int_distribution<int> dis(0, m); std::mt19937 gen_mt(n); std::string str(6, '\0'); for(int = 0; i<6; i++) str[i] = (char)('.' + dis(gen_mt)); return str; } int g(unsigned int n, int m){ std::uniform_int_distribution<int> dis(0, m); std::mt19937 gen_mt(n); return dis(gen_mt); } int main() { std::string s1 = fx(g(66730461, 90000000) + 400000000, 33) + "/" + fx(g(28989020, 90000000) * 10, 43); std::cout << s1 << std::endl; }
coliru.
...and again.
int main() { std::string s2 = fx(g(66730461, 90000000) + 400000000, 33) + "/" + fx(g(81141643, 90000000) + 100000000, 43); std::cout << s2 << std::endl; }
do meet kind of problem? or unlucky?
my sad friend, must inform in life have never encountered programmer poor luck have. chances of random string generator creating human-readable output 1 in million, managed 3 times in row (one in trillion?)
in honesty, trick quite clever. may future endeavors more predictable :)
(future readers: results generated using hand-picked pseudo-random seed values happened output specific strings, "hello world", etc... check out comments on question more info)
Comments
Post a Comment