 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Peter Ammon Guest
|
Posted: Wed Oct 29, 2003 10:40 pm Post subject: random_shuffle seed |
|
|
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?
Thanks,
-peter
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Oct 30, 2003 3:08 am Post subject: Re: random_shuffle seed |
|
|
"Peter Ammon" <peter_ammon (AT) rocketmail (DOT) com> wrote...
| Quote: | 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?
|
Read about 'srand' function.
V
|
|
| Back to top |
|
 |
Chris Theis Guest
|
Posted: Thu Oct 30, 2003 8:00 am Post subject: Re: random_shuffle seed |
|
|
"Peter Ammon" <peter_ammon (AT) rocketmail (DOT) com> wrote
| Quote: | 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?
Thanks,
-peter
|
Look up the srand() function. Although it seems stupid at first glance, a
generator producing "pseudo-random" numbers using always the same seed at
startup (e.g. without specifying a different seed every time you run the
program), will give you a reproducable sequence of "random" numbers. This is
very helpful in case you want to debug the behavior of a simulation for
example.
BTW you should use srand only once in your program!!
HTH
Chris
|
|
| Back to top |
|
 |
Peter Ammon Guest
|
Posted: Fri Oct 31, 2003 1:05 am Post subject: Re: random_shuffle seed |
|
|
Victor Bazarov wrote:
| Quote: | "Peter Ammon" <peter_ammon (AT) rocketmail (DOT) com> wrote...
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?
Read about 'srand' function.
V
|
Tried that and no dice. Looking in my headers, I see that
random_shuffle uses a template __random_number, which is defined as so:
template<typename _Distance>
inline _Distance
__random_number(_Distance __n)
{
#ifdef _GLIBCPP_HAVE_DRAND48
return lrand48() % __n;
#else
return rand() % __n;
#endif
}
and _GLIBCPP_HAVE_DRAND48 is unconditionally defined elsewhere, even
when I compile with -ansi -pedantic. I can seed it and get more random
results with seed48, but of course this is not portable.
So am I to conclude that there is no guarantee that random_shuffle's
internal PRNG is rand(), and that there need not be any user accessible
way to seed it? This would seriously call into question the utility of
random_shuffle without a user supplied RandomNumberGenerator in any
portable program
Ordinarily I'd call this a QoI issue, but I'm using g++ which is a
pretty high Q I.
-Peter
|
|
| Back to top |
|
 |
Peter Ammon Guest
|
Posted: Fri Oct 31, 2003 1:06 am Post subject: Re: random_shuffle seed |
|
|
Chris Theis wrote:
| Quote: | "Peter Ammon" <peter_ammon (AT) rocketmail (DOT) com> wrote in message
news:bnpfl8$mqb$1 (AT) news (DOT) apple.com...
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?
Thanks,
-peter
Look up the srand() function. Although it seems stupid at first glance, a
generator producing "pseudo-random" numbers using always the same seed at
startup (e.g. without specifying a different seed every time you run the
program), will give you a reproducable sequence of "random" numbers. This is
very helpful in case you want to debug the behavior of a simulation for
example.
BTW you should use srand only once in your program!!
HTH
Chris
|
random_shuffle evidently doesn't have to use rand() as its internal
PRNG; see my response to Mr. Bazarov.
-Peter
|
|
| Back to top |
|
 |
Chris Theis Guest
|
Posted: Fri Oct 31, 2003 8:21 am Post subject: Re: random_shuffle seed |
|
|
"Peter Ammon" <peter_ammon (AT) rocketmail (DOT) com> wrote
| Quote: | Victor Bazarov wrote:
[SNIP]
template
inline _Distance
__random_number(_Distance __n)
{
#ifdef _GLIBCPP_HAVE_DRAND48
return lrand48() % __n;
#else
return rand() % __n;
#endif
}
and _GLIBCPP_HAVE_DRAND48 is unconditionally defined elsewhere, even
when I compile with -ansi -pedantic. I can seed it and get more random
results with seed48, but of course this is not portable.
So am I to conclude that there is no guarantee that random_shuffle's
internal PRNG is rand(), and that there need not be any user accessible
way to seed it? This would seriously call into question the utility of
random_shuffle without a user supplied RandomNumberGenerator in any
portable program
|
It is enforced by the standard that random shuffle uses a uniform
distribution random number generator. It is not guaranteed that this is
rand(). Due to the fact rand() is a standard function available on every
system it is the practical choice, which is also portable. If your compiler
implements the use of a non-standard function like lrand48 it's good for you
getting better random numbers, although you'll have to seed it accordingly.
IMHO such approaches are arguable because the user should at least be
notified that a non portable function is used for a standard algorithm.
Anyway I'm not sure whether _GLIBCPP_HAVE_DRAND48 is defined by default even
on systems supplying lrand48. This is actually the first case that I've
heard that this was troublesome. Generally one should supply an
implementation of a reasonable good RNG if good random numbers are required.
Regards
Chris
|
|
| Back to top |
|
 |
DrPizza Guest
|
Posted: Sun Nov 02, 2003 3:45 am Post subject: Re: random_shuffle seed |
|
|
"Peter Ammon" <peter_ammon (AT) rocketmail (DOT) com> wrote
| Quote: | 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/wg21/docs/lwg-active.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;} /* [email]drpizza (AT) battleaxe (DOT) net[/email] brainf*** program as argv[1] */
|
|
| Back to top |
|
 |
Howard Hinnant Guest
|
Posted: Mon Nov 03, 2003 8:54 pm Post subject: Re: random_shuffle seed |
|
|
In article <TT_ob.101487$KB.71834 (AT) fe04 (DOT) atl2.webusenet.com>,
"DrPizza" <drpizza (AT) battleaxe (DOT) net> wrote:
| Quote: | "Peter Ammon" <peter_ammon (AT) rocketmail (DOT) com> wrote in message
news:bnpfl8$mqb$1 (AT) news (DOT) apple.com...
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/wg21/docs/lwg-active.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
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Thu Nov 06, 2003 1:38 am Post subject: Re: random_shuffle seed |
|
|
DrPizza wrote:
| Quote: |
"Peter Ammon" <peter_ammon (AT) rocketmail (DOT) com> wrote in message
news:bnpfl8$mqb$1 (AT) news (DOT) apple.com...
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/wg21/docs/lwg-active.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)
|
|
| Back to top |
|
 |
DrPizza Guest
|
Posted: Thu Nov 06, 2003 2:07 am Post subject: Re: random_shuffle seed |
|
|
"Pete Becker" <petebecker (AT) acm (DOT) org> wrote
| Quote: | DrPizza wrote:
"Peter Ammon" <peter_ammon (AT) rocketmail (DOT) com> wrote in message
news:bnpfl8$mqb$1 (AT) news (DOT) apple.com...
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/wg21/docs/lwg-active.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;} /* [email]drpizza (AT) battleaxe (DOT) net[/email] brainf*** program as argv[1] */
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Thu Nov 06, 2003 2:23 am Post subject: Re: random_shuffle seed |
|
|
DrPizza wrote:
| Quote: |
You wouldn't want to permit your C++ library to call strtok()
internally, would you?
|
Sure. Why not?
| Quote: | 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)
|
|
| Back to top |
|
 |
DrPizza Guest
|
Posted: Thu Nov 06, 2003 1:19 pm Post subject: Re: random_shuffle seed |
|
|
"Pete Becker" <petebecker (AT) acm (DOT) org> wrote
| Quote: | 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? |
| Quote: | 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;} /* [email]drpizza (AT) battleaxe (DOT) net[/email] brainf*** program as argv[1] */
|
|
| Back to top |
|
 |
tom_usenet Guest
|
Posted: Thu Nov 06, 2003 1:52 pm Post subject: Re: random_shuffle seed |
|
|
On Thu, 6 Nov 2003 13:19:32 -0000, "DrPizza"
<drpizza (AT) anti-flash (DOT) co.uk> wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Thu Nov 06, 2003 2:50 pm Post subject: Re: random_shuffle seed |
|
|
DrPizza wrote:
| Quote: |
"Pete Becker" <petebecker (AT) acm (DOT) org> wrote in message
news:3FA9B097.746AD23B (AT) acm (DOT) org...
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.
| Quote: |
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)
|
|
| Back to top |
|
 |
DrPizza Guest
|
Posted: Fri Nov 07, 2003 2:24 am Post subject: Re: random_shuffle seed |
|
|
"tom_usenet" <tom_usenet (AT) hotmail (DOT) com> wrote
| Quote: | 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.
| Quote: | 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;} /* [email]drpizza (AT) battleaxe (DOT) net[/email] brainf*** program as argv[1] */
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|