Title:
Open Graph Title:
X Title:
Description: Describe the bug Produces random floating-point values i, uniformly distributed on the interval [a, b) cppreference.com The below code sample demonstrates how a default-constructed uniform_real_distribution
Open Graph Description: Describe the bug Produces random floating-point values i, uniformly distributed on the interval [a, b) cppreference.com The below code sample demonstrates how a default-constructed uniform_real_dis...
X Description: Describe the bug Produces random floating-point values i, uniformly distributed on the interval [a, b) cppreference.com The below code sample demonstrates how a default-constructed uniform_real_dis...
Opengraph URL: https://github.com/microsoft/STL/issues/1074
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"\u003crandom\u003e: uniform_real_distribution(a, b) produces outputs outside [a, b)","articleBody":"**Describe the bug**\r\n\u003e Produces random floating-point values i, uniformly distributed on the interval [a, b)\r\n\r\n*[cppreference.com](https://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution)*\r\n\r\nThe below code sample demonstrates how a default-constructed `uniform_real_distribution\u003cfloat\u003e` (a=0, b=1) generates a `1.0f` if provided a random number with all bits set:\r\n\r\n**Command-line test case**\r\n```\r\n#include \u003ccassert\u003e\r\n#include \u003ccstdint\u003e\r\n#include \u003climits\u003e\r\n#include \u003crandom\u003e\r\n\r\nstruct max_rng\r\n{\r\n\tusing result_type = std::uint32_t;\r\n\tresult_type min() const { return 0; }\r\n\tresult_type max() const { return std::numeric_limits\u003cresult_type\u003e::max(); }\r\n\tresult_type operator()() { return max(); }\r\n};\r\n\r\nint main()\r\n{\r\n\tmax_rng rng;\r\n\tstd::uniform_real_distribution\u003cfloat\u003e iid;\r\n\r\n\tassert(iid(rng) \u003c 1.0f);\r\n\r\n\treturn 0;\r\n}\r\n```\r\n\r\n**Expected behavior**\r\nRegardless of the input received from the RNG, `uniform_real_distribution` should never produce `b` as the interval is supposed to be right-*exclusive*. GCC passes this test.\r\n\r\n**STL version**\r\nMicrosoft Visual Studio Community 2019\r\nVersion 16.6.4\r\n\r\n**Additional context**\r\nThe error seems to originate from [`generate_canonical`](https://github.com/microsoft/STL/blob/c10ae01b4d9508eed9d5f059a120ee7223b6ac12/stl/inc/random#L261), which, when passed in the above code, simplifies down to:\r\n\r\n```\r\nreturn (float) UINT_MAX / (UINT_MAX + 1.0f);\r\n```\r\nwhich, mathematically speaking, *should* be less than 1, but in practice gets rounded to `1.0f` due to limited floating point precision. `generate_canonical` is flawed in other regards: using a normalizing/scaling division leads to non-uniformly distributed values. Both of these issues could be avoided by generating random floats the \"canonical\" way, which is to set the bits of the mantissa directly (and simply throw away left-over entropy) along the lines of:\r\n```\r\n// @rnd: uniformly distributed 32bit unsigned int.\r\nreturn (rnd \u003e\u003e 8) / 164233216.0f\r\n```\r\nwhich not only produces values strictly from [0, 1) for any value of `rnd`, but also makes sure that all values are equally far spaced apart leading to a perfect uniform distribution.\r\n\r\nAlso tracked by DevCom-110322 and Microsoft-internal VSO-253526 / [AB#253526](https://devdiv.visualstudio.com/0bdbc590-a062-4c3f-b0f6-9383f67865ee/_workitems/edit/253526) .\r\n","author":{"url":"https://github.com/denniskb","@type":"Person","name":"denniskb"},"datePublished":"2020-07-22T00:13:58.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":7},"url":"https://github.com/1074/STL/issues/1074"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:51d563c6-b12d-5c75-63f0-5ba7366b37b9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B8BE:2BDD1C:3C8BBBE:52653A4:6A44D3F4 |
| html-safe-nonce | 8bb17e880a6fcb99ae294682b3a026ce55a3963e6388985acc4382c5ad253809 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOEJFOjJCREQxQzozQzhCQkJFOjUyNjUzQTQ6NkE0NEQzRjQiLCJ2aXNpdG9yX2lkIjoiMTcwNzA3MDA2MjUwNDIzNjAyMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d659b9acb7771ccf95e927be8be227a7bf7efb0dba9e8196666c1983e4a581d5 |
| hovercard-subject-tag | issue:663384573 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/STL/1074/issue_layout |
| twitter:image | https://opengraph.githubassets.com/579f5dc027de6bc8d2c42bb4940b7f98121b897cc8ab56c64e3c1030adda0053/microsoft/STL/issues/1074 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/579f5dc027de6bc8d2c42bb4940b7f98121b897cc8ab56c64e3c1030adda0053/microsoft/STL/issues/1074 |
| og:image:alt | Describe the bug Produces random floating-point values i, uniformly distributed on the interval [a, b) cppreference.com The below code sample demonstrates how a default-constructed uniform_real_dis... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | denniskb |
| hostname | github.com |
| expected-hostname | github.com |
| None | b35ebc59c0fb02c2210b71c251a0894afe181fe7b14c0c156ba1cedcaddc6fe4 |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/STL git https://github.com/microsoft/STL.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 204593825 |
| octolytics-dimension-repository_nwo | microsoft/STL |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 204593825 |
| octolytics-dimension-repository_network_root_nwo | microsoft/STL |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 6b5b645842c42bc7b218f10a0557cffe9f81c436 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width