Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: random_shuffle seed

Reply
Thread Tools

Re: random_shuffle seed

 
 
DrPizza
Guest
Posts: n/a
 
      11-02-2003
"Peter Ammon" <> wrote in message
news:bnpfl8$mqb$...
> I'm calling random_shuffle without passing in a RandomNumberGenerator
> and getting the same shuffle every time I restart my program. Apparently
> I need to seed the internal random number generator. But how? I can't
> find any way to do that, and if there is none, then how is the internal
> random number generator useful?


With difficulty.

The Standard provides no function to do this, and random_shuffle is forbidden
from using std::rand(). C++ "inherits" rand() from C (see [lib.c.math] -- rand
isn't defined in the C++ Standard at all, instead a reference is given to the
C Standard), and C states that "The implementation shall behave as if no
library function calls the rand function".

This constraint thus applies to C++, which thus prevents random_shuffle from
using rand().

A resolution is proposed.
http://anubis.dkuug.dk/jtc1/sc22/wg2...ctive.html#395

--
Now Playing: Marc Aurel - Running (dumonde remix) (D I G I T A L L Y - I M P
O R T E D - European Trance, Techno, Hi-NRG... we can't define it!)


char a[99999],*p=a;main(c,V)char**V;{char*v=c>0?1[V]:V;if(c)for(;(c=*v)&&93^
c;p+=!(62^c)-!(60^c),*p+=!(43^c)-!(45^c),44^c||read(0,p,1),46^c||putchar(*p)
,91^c||(v=*p?main(-1,v+1),v-1:main(0,v)),++v);else for(;c+=!(91^*v)-!(93^*v)
;++v);return v;} /* brainf*** program as argv[1] */



 
Reply With Quote
 
 
 
 
Howard Hinnant
Guest
Posts: n/a
 
      11-03-2003
In article <TT_ob.101487$>,
"DrPizza" <> wrote:

> "Peter Ammon" <> wrote in message
> news:bnpfl8$mqb$...
> > I'm calling random_shuffle without passing in a RandomNumberGenerator
> > and getting the same shuffle every time I restart my program. Apparently
> > I need to seed the internal random number generator. But how? I can't
> > find any way to do that, and if there is none, then how is the internal
> > random number generator useful?

>
> With difficulty.
>
> The Standard provides no function to do this, and random_shuffle is forbidden
> from using std::rand(). C++ "inherits" rand() from C (see [lib.c.math] -- rand
> isn't defined in the C++ Standard at all, instead a reference is given to the
> C Standard), and C states that "The implementation shall behave as if no
> library function calls the rand function".
>
> This constraint thus applies to C++, which thus prevents random_shuffle from
> using rand().
>
> A resolution is proposed.
> http://anubis.dkuug.dk/jtc1/sc22/wg2...ctive.html#395


And just fyi, the resolution was looked upon favoribly at last week's
standards meeting. If I remember correctly, its status was moved to
"ready". But don't take that to the bank until the next revision of the
issues list comes out.

-Howard
 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      11-06-2003
DrPizza wrote:
>
> "Peter Ammon" <> wrote in message
> news:bnpfl8$mqb$...
> > I'm calling random_shuffle without passing in a RandomNumberGenerator
> > and getting the same shuffle every time I restart my program. Apparently
> > I need to seed the internal random number generator. But how? I can't
> > find any way to do that, and if there is none, then how is the internal
> > random number generator useful?

>
> With difficulty.
>
> The Standard provides no function to do this, and random_shuffle is forbidden
> from using std::rand(). C++ "inherits" rand() from C (see [lib.c.math] -- rand
> isn't defined in the C++ Standard at all, instead a reference is given to the
> C Standard), and C states that "The implementation shall behave as if no
> library function calls the rand function".
>
> This constraint thus applies to C++, which thus prevents random_shuffle from
> using rand().
>
> A resolution is proposed.
> http://anubis.dkuug.dk/jtc1/sc22/wg2...ctive.html#395
>


Yup, we added redundant language to make it clear that statements in the
C standard about the C library are not constraints on the C++ library.
Shouldn't need to be said...

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
DrPizza
Guest
Posts: n/a
 
      11-06-2003
"Pete Becker" <> wrote in message
news:...
> DrPizza wrote:
> >
> > "Peter Ammon" <> wrote in message
> > news:bnpfl8$mqb$...
> > > I'm calling random_shuffle without passing in a RandomNumberGenerator
> > > and getting the same shuffle every time I restart my program. Apparently
> > > I need to seed the internal random number generator. But how? I can't
> > > find any way to do that, and if there is none, then how is the internal
> > > random number generator useful?

> >
> > With difficulty.
> >
> > The Standard provides no function to do this, and random_shuffle is

forbidden
> > from using std::rand(). C++ "inherits" rand() from C (see [lib.c.math] --

rand
> > isn't defined in the C++ Standard at all, instead a reference is given to

the
> > C Standard), and C states that "The implementation shall behave as if no
> > library function calls the rand function".
> >
> > This constraint thus applies to C++, which thus prevents random_shuffle

from
> > using rand().
> >
> > A resolution is proposed.
> > http://anubis.dkuug.dk/jtc1/sc22/wg2...ctive.html#395
> >

>
> Yup, we added redundant language to make it clear that statements in the
> C standard about the C library are not constraints on the C++ library.
> Shouldn't need to be said...


Why not? You wouldn't want to permit your C++ library to call strtok()
internally, would you? In general it seems quite desirable to carry that
constraint over from C; rand() is probably the only exception.

--
Now Playing: Accuface - Theme From Accuface


char a[99999],*p=a;main(c,V)char**V;{char*v=c>0?1[V]:V;if(c)for(;(c=*v)&&93^
c;p+=!(62^c)-!(60^c),*p+=!(43^c)-!(45^c),44^c||read(0,p,1),46^c||putchar(*p)
,91^c||(v=*p?main(-1,v+1),v-1:main(0,v)),++v);else for(;c+=!(91^*v)-!(93^*v)
;++v);return v;} /* brainf*** program as argv[1] */


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      11-06-2003
DrPizza wrote:
>
> You wouldn't want to permit your C++ library to call strtok()
> internally, would you?


Sure. Why not?

> In general it seems quite desirable to carry that
> constraint over from C; rand() is probably the only exception.
>


"Seems quite desirable" is not the same as "required by the standard."

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
DrPizza
Guest
Posts: n/a
 
      11-06-2003
"Pete Becker" <> wrote in message
news:...
> DrPizza wrote:
> >
> > You wouldn't want to permit your C++ library to call strtok()
> > internally, would you?

>
> Sure. Why not?

Because it means you can't use strtok() yourself?

> > In general it seems quite desirable to carry that
> > constraint over from C; rand() is probably the only exception.

> "Seems quite desirable" is not the same as "required by the standard."

But it (surely) is required by the standard because the standard just defers
to the C standard for the description of these functions, and one aspect of
the C standard pertinent to these functions is that the implementation shall
not call them.


--
char a[99999],*p=a;main(c,V)char**V;{char*v=c>0?1[V]:V;if(c)for(;(c=*v)&&93^
c;p+=!(62^c)-!(60^c),*p+=!(43^c)-!(45^c),44^c||read(0,p,1),46^c||putchar(*p)
,91^c||(v=*p?main(-1,v+1),v-1:main(0,v)),++v);else for(;c+=!(91^*v)-!(93^*v)
;++v);return v;} /* brainf*** program as argv[1] */


 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      11-06-2003
On Thu, 6 Nov 2003 13:19:32 -0000, "DrPizza"
<> wrote:

>> > In general it seems quite desirable to carry that
>> > constraint over from C; rand() is probably the only exception.

>> "Seems quite desirable" is not the same as "required by the standard."

>But it (surely) is required by the standard because the standard just defers
>to the C standard for the description of these functions, and one aspect of
>the C standard pertinent to these functions is that the implementation shall
>not call them.


The C standard only says that the implementation of the C library
won't call them.

Taking the original quote:
"The implementation shall behave as if no library function calls the
rand function"

The "library" referred to is the C library, not the C++ one. Just
because C++ includes the (slightly modified) C library doesn't mean
that it *is* the C library.

Tom
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      11-06-2003
DrPizza wrote:
>
> "Pete Becker" <> wrote in message
> news:...
> > DrPizza wrote:
> > >
> > > You wouldn't want to permit your C++ library to call strtok()
> > > internally, would you?

> >
> > Sure. Why not?

> Because it means you can't use strtok() yourself?


Non sequitur.

>
> > > In general it seems quite desirable to carry that
> > > constraint over from C; rand() is probably the only exception.

> > "Seems quite desirable" is not the same as "required by the standard."

> But it (surely) is required by the standard because the standard just defers
> to the C standard for the description of these functions, and one aspect of
> the C standard pertinent to these functions is that the implementation shall
> not call them.
>


The C++ standard does not change that constraint. No C function calls
rand.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
DrPizza
Guest
Posts: n/a
 
      11-07-2003
"tom_usenet" <> wrote in message
news:...
> The C standard only says that the implementation of the C library
> won't call them.

Well, no, it doesn't, it simply says that the implementation shall behave as
if no library function calls rand. It doesn't specify which library.

> Taking the original quote:
> "The implementation shall behave as if no library function calls the
> rand function"
>
> The "library" referred to is the C library, not the C++ one. Just
> because C++ includes the (slightly modified) C library doesn't mean
> that it *is* the C library.

Upon incorporation into C++, "the library" refers to the C++ library. The C++
library is the only library there is, so it's the only one it could refer to.


--
Now Playing: Daft Punk - Around The World


char a[99999],*p=a;main(c,V)char**V;{char*v=c>0?1[V]:V;if(c)for(;(c=*v)&&93^
c;p+=!(62^c)-!(60^c),*p+=!(43^c)-!(45^c),44^c||read(0,p,1),46^c||putchar(*p)
,91^c||(v=*p?main(-1,v+1),v-1:main(0,v)),++v);else for(;c+=!(91^*v)-!(93^*v)
;++v);return v;} /* brainf*** program as argv[1] */


 
Reply With Quote
 
DrPizza
Guest
Posts: n/a
 
      11-07-2003
"Pete Becker" <> wrote in message
news:...
> DrPizza wrote:
> >
> > "Pete Becker" <> wrote in message
> > news:...
> > > DrPizza wrote:
> > > >
> > > > You wouldn't want to permit your C++ library to call strtok()
> > > > internally, would you?
> > >
> > > Sure. Why not?

> > Because it means you can't use strtok() yourself?

> Non sequitur.

If the implementation can call strtok() then I can't be sure what state
strtok()'s static buffer is in -- it need not be in the state I left it in.
This renders strtok() useless. If I use it I risk ****ing up the library; if
the library uses it it risks ****ing up my program.

> The C++ standard does not change that constraint. No C function calls
> rand.

Except the "not called by the library" statement refers only to the library of
the implementation, not the C library. Given that in C++ the implementation
is one of C++, and the library is that of C++, the restriction should constain
the C++ library too.


--
Now Playing: Daft Punk - Around The World


char a[99999],*p=a;main(c,V)char**V;{char*v=c>0?1[V]:V;if(c)for(;(c=*v)&&93^
c;p+=!(62^c)-!(60^c),*p+=!(43^c)-!(45^c),44^c||read(0,p,1),46^c||putchar(*p)
,91^c||(v=*p?main(-1,v+1),v-1:main(0,v)),++v);else for(;c+=!(91^*v)-!(93^*v)
;++v);return v;} /* brainf*** program as argv[1] */


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem binding a predicate to random_shuffle() Nelis Franken C++ 2 08-22-2006 03:20 AM
Stratified random sampling with random_shuffle ? steflhermitte C++ 1 04-21-2005 12:54 AM
STL function random_shuffle() possible bug? Ronak C++ 1 11-17-2003 11:48 AM
Re: random_shuffle seed Peter Ammon C++ 1 10-31-2003 08:21 AM
Re: random_shuffle seed Peter Ammon C++ 0 10-31-2003 01:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57