{"id":626,"date":"2015-01-09T17:36:49","date_gmt":"2015-01-09T16:36:49","guid":{"rendered":"http:\/\/numbercrunch.de\/blog\/?p=626"},"modified":"2023-01-18T22:14:34","modified_gmt":"2023-01-18T21:14:34","slug":"c1114-for-scientific-computing-iv","status":"publish","type":"post","link":"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/","title":{"rendered":"C++11\/14 for scientific computing IV"},"content":{"rendered":"<h2>Random numbers<\/h2>\n<p style=\"text-align: justify;\">C++98 inherited from C the standard functions <code>rand<\/code> and <code>srand<\/code> and the macro <code>RAND_MAX<\/code> for generating pseudo-random numbers. These functions suffer several problems. For example, it has been never specified which algorithm is used in <code>rand<\/code> to produce pseudo-random numbers. Thus, in C and in C++98 the outcome of a Monte Carlo simulation depends on the employed implementation of the standard library if <code>rand<\/code> is used. Furthermore, in C++98 it is hard to generate random numbers from some non-uniform distribution.<\/p>\n<p style=\"text-align: justify;\">Therefore, in C++11 a portable random number generator facility has been introduced. All classes related to random number generation are declared in the header <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/numeric\/random\" target=\"_blank\" rel=\"noopener\"><code>random<\/code><\/a>. The random number generator facility consists of engines and distributions. Engines produce streams of pseudo random bit patterns and may implement various algorithms for random number generation, e.g., <a href=\"http:\/\/en.wikipedia.org\/wiki\/Linear_congruential_generator\" target=\"_blank\" rel=\"noopener\">linear congruences<\/a> or the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Mersenne_twister\" target=\"_blank\" rel=\"noopener\">Mersenne Twister<\/a>. Distributions consume an engine&#8217;s bits to generate random numbers, which are distributed according to a specific distribution, e.g., uniform, exponential or Gaussian. The following code gives a simple example. A more detailed explanation of the C++11 random number generator facility can be found in <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2013\/n3551.pdf\" target=\"_blank\" rel=\"noopener\">Random Number Generation in C++11<\/a> and in the video <a href=\"http:\/\/channel9.msdn.com\/Events\/GoingNative\/2013\/rand-Considered-Harmful\" target=\"_blank\" rel=\"noopener\">rand() Considered Harmful<\/a>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;iostream&gt;\n#include &lt;random&gt;\n\nint main() {\n  \/\/ Mersenne Twister with default seed\n  std::mt19937 engine;\n  \/\/ exponential distribution with mean 1\n  std::exponential_distribution&lt;&gt; distribution(1);\n  for (int i(0); i&lt;100; ++i) \n    std::cout &lt;&lt; distribution(engine) &lt;&lt; '\\n';\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Random numbers C++98 inherited from C the standard functions rand and srand and the macro RAND_MAX for generating pseudo-random numbers. These functions suffer several problems. For example, it has been never specified which algorithm is used in rand to produce pseudo-random numbers. Thus, in C and in C++98 the outcome of a Monte Carlo simulation&hellip; <a href=\"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">C++11\/14 for scientific computing IV<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-626","post","type-post","status-publish","format-standard","hentry","category-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++11\/14 for scientific computing IV - Number Crunch<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++11\/14 for scientific computing IV - Number Crunch\" \/>\n<meta property=\"og:description\" content=\"Random numbers C++98 inherited from C the standard functions rand and srand and the macro RAND_MAX for generating pseudo-random numbers. These functions suffer several problems. For example, it has been never specified which algorithm is used in rand to produce pseudo-random numbers. Thus, in C and in C++98 the outcome of a Monte Carlo simulation&hellip; Continue reading C++11\/14 for scientific computing IV\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/\" \/>\n<meta property=\"og:site_name\" content=\"Number Crunch\" \/>\n<meta property=\"article:published_time\" content=\"2015-01-09T16:36:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-18T21:14:34+00:00\" \/>\n<meta name=\"author\" content=\"Heiko Bauke\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Heiko Bauke\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/\"},\"author\":{\"name\":\"Heiko Bauke\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413\"},\"headline\":\"C++11\/14 for scientific computing IV\",\"datePublished\":\"2015-01-09T16:36:49+00:00\",\"dateModified\":\"2023-01-18T21:14:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/\"},\"wordCount\":199,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413\"},\"articleSection\":[\"C++\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/\",\"url\":\"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/\",\"name\":\"C++11\/14 for scientific computing IV - Number Crunch\",\"isPartOf\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#website\"},\"datePublished\":\"2015-01-09T16:36:49+00:00\",\"dateModified\":\"2023-01-18T21:14:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.numbercrunch.de\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++11\/14 for scientific computing IV\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#website\",\"url\":\"https:\/\/www.numbercrunch.de\/blog\/\",\"name\":\"Number Crunch\",\"description\":\"A computational science blog.\",\"publisher\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.numbercrunch.de\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413\",\"name\":\"Heiko Bauke\",\"logo\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++11\/14 for scientific computing IV - Number Crunch","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/","og_locale":"en_US","og_type":"article","og_title":"C++11\/14 for scientific computing IV - Number Crunch","og_description":"Random numbers C++98 inherited from C the standard functions rand and srand and the macro RAND_MAX for generating pseudo-random numbers. These functions suffer several problems. For example, it has been never specified which algorithm is used in rand to produce pseudo-random numbers. Thus, in C and in C++98 the outcome of a Monte Carlo simulation&hellip; Continue reading C++11\/14 for scientific computing IV","og_url":"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/","og_site_name":"Number Crunch","article_published_time":"2015-01-09T16:36:49+00:00","article_modified_time":"2023-01-18T21:14:34+00:00","author":"Heiko Bauke","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Heiko Bauke","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/#article","isPartOf":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/"},"author":{"name":"Heiko Bauke","@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413"},"headline":"C++11\/14 for scientific computing IV","datePublished":"2015-01-09T16:36:49+00:00","dateModified":"2023-01-18T21:14:34+00:00","mainEntityOfPage":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/"},"wordCount":199,"commentCount":0,"publisher":{"@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413"},"articleSection":["C++"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/","url":"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/","name":"C++11\/14 for scientific computing IV - Number Crunch","isPartOf":{"@id":"https:\/\/www.numbercrunch.de\/blog\/#website"},"datePublished":"2015-01-09T16:36:49+00:00","dateModified":"2023-01-18T21:14:34+00:00","breadcrumb":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.numbercrunch.de\/blog\/2015\/01\/c1114-for-scientific-computing-iv\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.numbercrunch.de\/blog\/"},{"@type":"ListItem","position":2,"name":"C++11\/14 for scientific computing IV"}]},{"@type":"WebSite","@id":"https:\/\/www.numbercrunch.de\/blog\/#website","url":"https:\/\/www.numbercrunch.de\/blog\/","name":"Number Crunch","description":"A computational science blog.","publisher":{"@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.numbercrunch.de\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413","name":"Heiko Bauke","logo":{"@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/posts\/626"}],"collection":[{"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/comments?post=626"}],"version-history":[{"count":4,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/posts\/626\/revisions"}],"predecessor-version":[{"id":968,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/posts\/626\/revisions\/968"}],"wp:attachment":[{"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/media?parent=626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/categories?post=626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/tags?post=626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}