Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

tr1 <random> bug

10 views
Skip to first unread message

Daniel Lidström

unread,
Jan 12, 2009, 3:43:45 AM1/12/09
to
Hello,

I have discovered what seems to be a bug in the <random> header. Have a look
at this sample program:

#include <random>
#include <iostream>

int main()
{
// Uniform distribution using mt19937 generator with 2^19937-1 cycle.
typedef std::tr1::variate_generator<std::tr1::mt19937&,
std::tr1::uniform_int<int>> UniformReal6419937;
std::tr1::uniform_int<int> distr(0, 2);
std::tr1::mt19937 mt19937;
UniformReal6419937 generator(mt19937, distr);

for( int i=0; i<20; i++ )
{
std::cout << generator() << std::endl;
}

return 0;
}

I expect the output to be a mixture of 0, 1, and 2. However, this is what is
printed:
0
0
0
0
0
0
0
0
-1
0
0
-1
0
0
-1
0
0
0
0
0

Changing to boost writes this:
1
1
0
0
2
2
2
2
2
0
0
2
2
0
2
2
2
0
1
1

This might have been reported already, otherwise maybe one of the MVPs could
file it to Microsoft?
Thanks in advance!

--
Daniel

Stephan T. Lavavej (MSFT)

unread,
Jan 12, 2009, 7:51:20 PM1/12/09
to
Hi,

variate_generator was broken in VC9 SP1. We hotfixed this
(http://blogs.msdn.com/vcblog/archive/2008/12/17/vc9-sp1-hotfix-for-the-vector-function-ft-crash.aspx
), but the hotfix package was broken, so we're regenerating it.

However, I once tried to use variate_generator<mt19937, uniform_int<int> >,
and P.J. Plauger (PJP) told me this:

"The program is ill formed, according to TR1, because you can't convert all
values of the engine type (unsigned long) to the distribution type (int).
Converting the engine max() to int makes it -1, which leads to a zero divide
and +/- Inf generated values, which then get mapped to the whimsical int
values.

Note that variate_generator has been removed from C++0X."

As you may know, Microsoft has licensed its C++ Standard Library and TR1
implementations from Dinkumware; PJP maintains Dinkumware's implementations,
while I work with him to integrate his changes into Microsoft's sources.

Stephan T. Lavavej
Visual C++ Libraries Developer

"Daniel Lidström" <som...@microsoft.com> wrote in message
news:eAhmiHJd...@TK2MSFTNGP03.phx.gbl...

Daniel Lidström

unread,
Jan 13, 2009, 4:01:11 AM1/13/09
to
"Stephan T. Lavavej (MSFT)" <s...@microsoft.com> wrote in message
news:OZFlNkRd...@TK2MSFTNGP03.phx.gbl...

> Hi,
>
> variate_generator was broken in VC9 SP1. We hotfixed this
> (http://blogs.msdn.com/vcblog/archive/2008/12/17/vc9-sp1-hotfix-for-the-vector-function-ft-crash.aspx
> ), but the hotfix package was broken, so we're regenerating it.
>
> However, I once tried to use variate_generator<mt19937, uniform_int<int>
> >, and P.J. Plauger (PJP) told me this:
>
> "The program is ill formed, according to TR1, because you can't convert
> all values of the engine type (unsigned long) to the distribution type
> (int).
> Converting the engine max() to int makes it -1, which leads to a zero
> divide and +/- Inf generated values, which then get mapped to the
> whimsical int values.
>
> Note that variate_generator has been removed from C++0X."
>
> As you may know, Microsoft has licensed its C++ Standard Library and TR1
> implementations from Dinkumware; PJP maintains Dinkumware's
> implementations, while I work with him to integrate his changes into
> Microsoft's sources.
>
> Stephan T. Lavavej
> Visual C++ Libraries Developer


Many thanks for your answer Stephan.

--
Daniel

0 new messages