Bug 44653 - A uniform_real random distribution produces invalid floating-point random numbers
Summary: A uniform_real random distribution produces invalid floating-point random num...
Status: RESOLVED DUPLICATE of bug 40263
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.3.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-06-24 09:28 UTC by Dmitry Chichkov
Modified: 2010-06-24 10:00 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dmitry Chichkov 2010-06-24 09:28:26 UTC
According to: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1452.html

[quote] A uniform_real random distribution produces floating-point random numbers x in the range min <= x <= max, with equal probability. min and max are the parameters of the distribution. [/unqoute]


Following code (with either -std=c++0x or -std=gnu++0x):
#include <tr1/random>
#include <iostream>

int main()
{
 std::tr1::mt19937 e;
 std::tr1::uniform_real<double> dist(0.0, 1.0);
 std::cout << dist(e) << std::endl;
 return 0;
}

Produces: 3.49921e+09. It should produce a value between [0.0, 1.0). Not some wild int.
Comment 1 Dmitry Chichkov 2010-06-24 09:53:50 UTC
Update. Following code produces the correct value:

#include <functional>
#include <random>
#include <iostream>

int main()
{
 typedef std::mt19937 eng_t;
 typedef std::uniform_real<double> dist_t;
 typedef std::variate_generator <eng_t, dist_t > gen_t;
 eng_t eng;
 dist_t dist(0.0, 1.0);
 gen_t gen(eng, dist);

 std::cout << gen() << std::endl;
 return 0;
}

Comment 2 Paolo Carlini 2010-06-24 09:57:16 UTC
Indeed, that is what you should use for the TR1 random. Note that we have been trying to also deliver a conforming C++0x (thus, in std::) <random> only lately, in 4.3.x we delivered the TR1 code in the std:: namespace too. Thus, if you want to experiment with the C++0x version, use 4.5.x or mainline.
Comment 3 Paolo Carlini 2010-06-24 10:00:04 UTC
Reopen...
Comment 4 Paolo Carlini 2010-06-24 10:00:32 UTC
... to resolve as duplicate.

*** This bug has been marked as a duplicate of 40263 ***