random_shuffle seed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Ammon

    #1

    random_shuffle seed

    I'm calling random_shuffle without passing in a RandomNumberGen erator
    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

  • Victor Bazarov

    #2
    Re: random_shuffle seed

    "Peter Ammon" <peter_ammon@ro cketmail.com> wrote...[color=blue]
    > I'm calling random_shuffle without passing in a RandomNumberGen erator
    > 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?[/color]

    Read about 'srand' function.

    V


    Comment

    • Chris Theis

      #3
      Re: random_shuffle seed


      "Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
      news:bnpfl8$mqb $1@news.apple.c om...[color=blue]
      > I'm calling random_shuffle without passing in a RandomNumberGen erator
      > 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[/color]

      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


      Comment

      • Peter Ammon

        #4
        Re: random_shuffle seed

        Chris Theis wrote:
        [color=blue]
        > "Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
        > news:bnpfl8$mqb $1@news.apple.c om...
        >[color=green]
        >>I'm calling random_shuffle without passing in a RandomNumberGen erator
        >>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[/color]
        >
        >
        > 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[/color]

        random_shuffle evidently doesn't have to use rand() as its internal
        PRNG; see my response to Mr. Bazarov.

        -Peter

        Comment

        • Peter Ammon

          #5
          Re: random_shuffle seed

          Victor Bazarov wrote:
          [color=blue]
          > "Peter Ammon" <peter_ammon@ro cketmail.com> wrote...
          >[color=green]
          >>I'm calling random_shuffle without passing in a RandomNumberGen erator
          >>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?[/color]
          >
          >
          > Read about 'srand' function.
          >
          > V[/color]

          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<typena me _Distance>
          inline _Distance
          __random_number (_Distance __n)
          {
          #ifdef _GLIBCPP_HAVE_D RAND48
          return lrand48() % __n;
          #else
          return rand() % __n;
          #endif
          }

          and _GLIBCPP_HAVE_D RAND48 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 RandomNumberGen erator 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

          Comment

          • Chris Theis

            #6
            Re: random_shuffle seed


            "Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
            news:bnscih$14m $1@news.apple.c om...[color=blue]
            > Victor Bazarov wrote:
            >[/color]
            [SNIP][color=blue]
            >
            > template<typena me _Distance>
            > inline _Distance
            > __random_number (_Distance __n)
            > {
            > #ifdef _GLIBCPP_HAVE_D RAND48
            > return lrand48() % __n;
            > #else
            > return rand() % __n;
            > #endif
            > }
            >
            > and _GLIBCPP_HAVE_D RAND48 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 RandomNumberGen erator in any
            > portable program
            >[/color]

            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_D RAND48 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






            Comment

            • DrPizza

              #7
              Re: random_shuffle seed

              "Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
              news:bnpfl8$mqb $1@news.apple.c om...[color=blue]
              > I'm calling random_shuffle without passing in a RandomNumberGen erator
              > 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?[/color]

              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.


              --
              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+=!(4 3^c)-!(45^c),44^c||r ead(0,p,1),46^c ||putchar(*p)
              ,91^c||(v=*p?ma in(-1,v+1),v-1:main(0,v)),++ v);else for(;c+=!(91^*v )-!(93^*v)
              ;++v);return v;} /* drpizza@battlea xe.net brainf*** program as argv[1] */



              Comment

              • Howard Hinnant

                #8
                Re: random_shuffle seed

                In article <TT_ob.101487$K B.71834@fe04.at l2.webusenet.co m>,
                "DrPizza" <drpizza@battle axe.net> wrote:
                [color=blue]
                > "Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
                > news:bnpfl8$mqb $1@news.apple.c om...[color=green]
                > > I'm calling random_shuffle without passing in a RandomNumberGen erator
                > > 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?[/color]
                >
                > 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[/color]

                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

                Comment

                • Pete Becker

                  #9
                  Re: random_shuffle seed

                  DrPizza wrote:[color=blue]
                  >
                  > "Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
                  > news:bnpfl8$mqb $1@news.apple.c om...[color=green]
                  > > I'm calling random_shuffle without passing in a RandomNumberGen erator
                  > > 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?[/color]
                  >
                  > 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
                  >[/color]

                  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)

                  Comment

                  • DrPizza

                    #10
                    Re: random_shuffle seed

                    "Pete Becker" <petebecker@acm .org> wrote in message
                    news:3FA9A626.8 55D5039@acm.org ...[color=blue]
                    > DrPizza wrote:[color=green]
                    > >
                    > > "Peter Ammon" <peter_ammon@ro cketmail.com> wrote in message
                    > > news:bnpfl8$mqb $1@news.apple.c om...[color=darkred]
                    > > > I'm calling random_shuffle without passing in a RandomNumberGen erator
                    > > > 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?[/color]
                    > >
                    > > With difficulty.
                    > >
                    > > The Standard provides no function to do this, and random_shuffle is[/color][/color]
                    forbidden[color=blue][color=green]
                    > > from using std::rand(). C++ "inherits" rand() from C (see [lib.c.math] --[/color][/color]
                    rand[color=blue][color=green]
                    > > isn't defined in the C++ Standard at all, instead a reference is given to[/color][/color]
                    the[color=blue][color=green]
                    > > 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[/color][/color]
                    from[color=blue][color=green]
                    > > using rand().
                    > >
                    > > A resolution is proposed.
                    > > http://anubis.dkuug.dk/jtc1/sc22/wg2...ctive.html#395
                    > >[/color]
                    >
                    > 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...[/color]

                    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+=!(4 3^c)-!(45^c),44^c||r ead(0,p,1),46^c ||putchar(*p)
                    ,91^c||(v=*p?ma in(-1,v+1),v-1:main(0,v)),++ v);else for(;c+=!(91^*v )-!(93^*v)
                    ;++v);return v;} /* drpizza@battlea xe.net brainf*** program as argv[1] */


                    Comment

                    • Pete Becker

                      #11
                      Re: random_shuffle seed

                      DrPizza wrote:[color=blue]
                      >
                      > You wouldn't want to permit your C++ library to call strtok()
                      > internally, would you?[/color]

                      Sure. Why not?
                      [color=blue]
                      > In general it seems quite desirable to carry that
                      > constraint over from C; rand() is probably the only exception.
                      >[/color]

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

                      --

                      Pete Becker
                      Dinkumware, Ltd. (http://www.dinkumware.com)

                      Comment

                      • DrPizza

                        #12
                        Re: random_shuffle seed

                        "Pete Becker" <petebecker@acm .org> wrote in message
                        news:3FA9B097.7 46AD23B@acm.org ...[color=blue]
                        > DrPizza wrote:[color=green]
                        > >
                        > > You wouldn't want to permit your C++ library to call strtok()
                        > > internally, would you?[/color]
                        >
                        > Sure. Why not?[/color]
                        Because it means you can't use strtok() yourself?
                        [color=blue][color=green]
                        > > In general it seems quite desirable to carry that
                        > > constraint over from C; rand() is probably the only exception.[/color]
                        > "Seems quite desirable" is not the same as "required by the standard."[/color]
                        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+=!(4 3^c)-!(45^c),44^c||r ead(0,p,1),46^c ||putchar(*p)
                        ,91^c||(v=*p?ma in(-1,v+1),v-1:main(0,v)),++ v);else for(;c+=!(91^*v )-!(93^*v)
                        ;++v);return v;} /* drpizza@battlea xe.net brainf*** program as argv[1] */


                        Comment

                        • tom_usenet

                          #13
                          Re: random_shuffle seed

                          On Thu, 6 Nov 2003 13:19:32 -0000, "DrPizza"
                          <drpizza@anti-flash.co.uk> wrote:
                          [color=blue][color=green][color=darkred]
                          >> > In general it seems quite desirable to carry that
                          >> > constraint over from C; rand() is probably the only exception.[/color]
                          >> "Seems quite desirable" is not the same as "required by the standard."[/color]
                          >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.[/color]

                          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

                          Comment

                          • Pete Becker

                            #14
                            Re: random_shuffle seed

                            DrPizza wrote:[color=blue]
                            >
                            > "Pete Becker" <petebecker@acm .org> wrote in message
                            > news:3FA9B097.7 46AD23B@acm.org ...[color=green]
                            > > DrPizza wrote:[color=darkred]
                            > > >
                            > > > You wouldn't want to permit your C++ library to call strtok()
                            > > > internally, would you?[/color]
                            > >
                            > > Sure. Why not?[/color]
                            > Because it means you can't use strtok() yourself?[/color]

                            Non sequitur.
                            [color=blue]
                            >[color=green][color=darkred]
                            > > > In general it seems quite desirable to carry that
                            > > > constraint over from C; rand() is probably the only exception.[/color]
                            > > "Seems quite desirable" is not the same as "required by the standard."[/color]
                            > 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.
                            >[/color]

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

                            --

                            Pete Becker
                            Dinkumware, Ltd. (http://www.dinkumware.com)

                            Comment

                            • DrPizza

                              #15
                              Re: random_shuffle seed

                              "tom_usenet " <tom_usenet@hot mail.com> wrote in message
                              news:nckkqvsrn5 lghoe2ire026mqv qlvqsr332@4ax.c om...[color=blue]
                              > The C standard only says that the implementation of the C library
                              > won't call them.[/color]
                              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.
                              [color=blue]
                              > 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.[/color]
                              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+=!(4 3^c)-!(45^c),44^c||r ead(0,p,1),46^c ||putchar(*p)
                              ,91^c||(v=*p?ma in(-1,v+1),v-1:main(0,v)),++ v);else for(;c+=!(91^*v )-!(93^*v)
                              ;++v);return v;} /* drpizza@battlea xe.net brainf*** program as argv[1] */


                              Comment

                              Working...