{"id":2455,"date":"2016-01-14T16:35:43","date_gmt":"2016-01-14T15:35:43","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=2455"},"modified":"2016-01-14T17:05:47","modified_gmt":"2016-01-14T16:05:47","slug":"stdnormal_distribution","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=2455","title":{"rendered":"std::normal_distribution"},"content":{"rendered":"<p>Wie baut man aus gleichverteilen Zufallszahlen normalverteile?<br \/>\nMan nimmt std::normal_distribution<\/p>\n<p>http:\/\/en.cppreference.com\/w\/cpp\/numeric\/random\/normal_distribution<\/p>\n<p>Und wie ist das Implementiert? Eine Beispielimplementation gibt es in der gnu stdc++<\/p>\n<p>https:\/\/gcc.gnu.org\/onlinedocs\/gcc-4.7.4\/libstdc++\/api\/a01267_source.html#l01678<\/p>\n<p>Wtf? Versteht kein Mesch. Das Paper auf dass die sich beziehen ist auch ein Hammer.<\/p>\n<p>http:\/\/www.eirene.de\/Devroye.pdf Seite 249<\/p>\n<p>Hier die Template bereinigte Version vom GNU<\/p>\n<pre><code>\r\n#include &lt;iostream>\r\n#include &lt;iomanip>\r\n#include &lt;string>\r\n#include &lt;map>\r\n#include &lt;random>\r\n#include &lt;cmath>\r\n\r\ndouble round(double x) {\r\n    return int(x*10)\/10.0;\r\n}\r\n\r\n\/\/ erzeugt gleichverteilte zufallzahlen im interfall [0:1]\r\nstruct uniform_real_random_device {\r\n    std::mt19937 gen;\r\n    std::uniform_real_distribution&lt;> uniform_dist = std::uniform_real_distribution&lt;>(0.0, 1.0);\r\n\r\n    double operator() () {\r\n        return uniform_dist(gen);\r\n    }\r\n};\r\n\r\n\/**\r\n  * Polar method due to Marsaglia.\r\n  *\r\n  * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag,\r\n  * New York, 1986, Ch. V, Sect. 4.4.\r\n  *\r\n  * https:\/\/gcc.gnu.org\/onlinedocs\/gcc-4.7.4\/libstdc++\/api\/a01267_source.html#l01678\r\n  *\/\r\ndouble normal_dist(uniform_real_random_device& rand, double mean, double stddev) {\r\n    double x, y, r2;\r\n    do\r\n    {\r\n        x = 2.0 * rand() - 1.0;\r\n        y = 2.0 * rand() - 1.0;\r\n        r2 = x * x + y * y;\r\n    }\r\n    while (r2 > 1.0 || r2 == 0.0);\r\n\r\n    double mult = std::sqrt(-2 * std::log(r2) \/ r2);\r\n    double result = y * mult;\r\n    result = result*stddev + mean;\r\n    return result;\r\n}\r\n\r\nint main() {\r\n    uniform_real_random_device uniformRand;\r\n\r\n    std::map<double, int> hist;\r\n    for(int n=0; n<100000; ++n) {\r\n        double gaussZahle = normal_dist(uniformRand, 0.5, 0.5);\r\n        ++hist[round(gaussZahle)];\r\n    }\r\n\r\n    \/\/ ausgabe\r\n    for(auto p : hist) {\r\n        std::cout << std::fixed << std::setprecision(1) << std::setw(2)\r\n                  << p.first << ' ' << std::string(p.second\/100, '*') << '\\n';\r\n    }\r\n}\r\n<\/code><\/pre>\n<blockquote><p>\n-1.5<br \/>\n-1.4<br \/>\n-1.3<br \/>\n-1.2<br \/>\n-1.1<br \/>\n-1.0<br \/>\n-0.9 *<br \/>\n-0.8 *<br \/>\n-0.7 ***<br \/>\n-0.6 *****<br \/>\n-0.5 ********<br \/>\n-0.4 ************<br \/>\n-0.3 ******************<br \/>\n-0.2 **************************<br \/>\n-0.1 **********************************<br \/>\n0.0 ************************************************************************************************<br \/>\n0.1 **************************************************************<br \/>\n0.2 *********************************************************************<br \/>\n0.3 ****************************************************************************<br \/>\n0.4 *******************************************************************************<br \/>\n0.5 *******************************************************************************<br \/>\n0.6 ****************************************************************************<br \/>\n0.7 **********************************************************************<br \/>\n0.8 *************************************************************<br \/>\n0.9 *****************************************************<br \/>\n1.0 *******************************************<br \/>\n1.1 **********************************<br \/>\n1.2 **************************<br \/>\n1.3 ******************<br \/>\n1.4 *************<br \/>\n1.5 *********<br \/>\n1.6 *****<br \/>\n1.7 ***<br \/>\n1.8 **<br \/>\n1.9 *<br \/>\n2.0<br \/>\n2.1<br \/>\n2.2<br \/>\n2.3<br \/>\n2.4<br \/>\n2.5\n<\/p><\/blockquote>\n<p>Jaaa da ist noch ein Bug drin bei der 0. Wer ihn findet, darf mir eine Mail schreiben.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wie baut man aus gleichverteilen Zufallszahlen normalverteile? Man nimmt std::normal_distribution http:\/\/en.cppreference.com\/w\/cpp\/numeric\/random\/normal_distribution Und wie ist das Implementiert? Eine Beispielimplementation gibt es in der gnu stdc++ https:\/\/gcc.gnu.org\/onlinedocs\/gcc-4.7.4\/libstdc++\/api\/a01267_source.html#l01678 Wtf? Versteht kein Mesch. Das Paper auf dass die sich beziehen ist auch ein Hammer. http:\/\/www.eirene.de\/Devroye.pdf Seite 249 Hier die Template bereinigte Version vom GNU #include &lt;iostream> #include &lt;iomanip> #include [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17],"class_list":["post-2455","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-cpp"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2455"}],"version-history":[{"count":3,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2455\/revisions"}],"predecessor-version":[{"id":2458,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2455\/revisions\/2458"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2455"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}