random number geneator

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

    random number geneator

    Hi all:
    I am trying to write a simple program that simulates asking several persons
    their birth day and it counts how many persons are asked until two have the
    same birth day. The problem that I have is that the first loop I get a
    sequence of random numbers untuil I get a match, BUT then on the following
    loops I get the SAME random(?) sequence. I am using rand(). I do not want to
    get too fancy with the random number generator, but is there a way of
    stopping rand() from resetting every time it starts the loop? What other
    choices do I have? Can I somehow use the system time to help me get more
    random? Here is my code so far:

    #include <iostream>
    #include <cstdlib>

    using namespace std;

    int main()
    {
    int const tests = 10000;
    int index;
    int i,j;
    int match = 0;
    int days[366] = {0};
    int count = 0;

    for (j = 1; j <= tests; j++)
    {
    while (match != 1)
    {
    index = (rand() % 365) + 1;

    if (days[index] != 1)
    {
    days[index]++;
    count++;
    }
    else
    {
    for (i = 1; i < 366; i++)
    {
    days[i] = 0;
    }

    match = 1;
    }

    }

    cout << count << endl;
    }

    return 0;
    }


  • Bruce

    #2
    Re: random number geneator

    In comp.lang.c++
    "Sonoman" <sonoman@aol.co m> wrote:
    [color=blue]
    >get too fancy with the random number generator, but is there a way of
    >stopping rand() from resetting every time it starts the loop? What other
    >choices do I have? Can I somehow use the system time to help me get more
    >random?[/color]

    Yes, add ONE call like this:

    srand(time(NULL ));

    and your done.

    Comment

    • Pierre Espenan

      #3
      Re: random number geneator


      ----- Original Message -----
      From: "Sonoman" <sonoman@aol.co m>
      Newsgroups: comp.lang.c++
      Sent: Wednesday, January 07, 2004 6:25 PM
      Subject: random number geneator

      [color=blue]
      > Hi all:
      > I am trying to write a simple program that simulates asking several[/color]
      persons[color=blue]
      > their birth day and it counts how many persons are asked until two have[/color]
      the[color=blue]
      > same birth day. The problem that I have is that the first loop I get a
      > sequence of random numbers untuil I get a match, BUT then on the following
      > loops I get the SAME random(?) sequence. I am using rand(). I do not want[/color]
      to[color=blue]
      > get too fancy with the random number generator, but is there a way of
      > stopping rand() from resetting every time it starts the loop? What other
      > choices do I have? Can I somehow use the system time to help me get more
      > random? Here is my code so far:
      >
      > #include <iostream>
      > #include <cstdlib>[/color]

      add the header, <ctime>
      [color=blue]
      >
      > using namespace std;
      >
      > int main()
      > {
      > int const tests = 10000;
      > int index;
      > int i,j;
      > int match = 0;
      > int days[366] = {0};
      > int count = 0;
      >
      > for (j = 1; j <= tests; j++)
      > {
      > while (match != 1)[/color]

      at the begining of the loop add,
      "srand(clock()) ;"
      [color=blue]
      > {
      > index = (rand() % 365) + 1;
      >
      > if (days[index] != 1)
      > {
      > days[index]++;
      > count++;
      > }
      > else
      > {
      > for (i = 1; i < 366; i++)
      > {
      > days[i] = 0;
      > }
      >
      > match = 1;
      > }
      >
      > }[/color]
      Here you need to put something to change the "seed" in the
      "srand(clock()) ;" statement, for example a counter that
      requires <Enter> to be pressed every 10 loops
      if(counter%10== 9) cin.get();[color=blue]
      >
      > cout << count << endl;
      > }[/color]

      I think you wanted the last "}" bracket above "cout << count << endl;"
      [color=blue]
      > return 0;
      > }
      >
      >[/color]

      Keep in mind that these are still pseudorandom numbers, but at least the
      seed will be changed by pressing the <Enter>.


      Comment

      • Chris Theis

        #4
        Re: random number geneator


        "Pierre Espenan" <whistling_rabb it@yahoo.com> wrote in message
        news:zi6Lb.4460 4$Pg1.28552@new sread1.news.pas .earthlink.net. ..[color=blue]
        >[/color]
        [SNIP][color=blue][color=green]
        > >
        > > for (j = 1; j <= tests; j++)
        > > {
        > > while (match != 1)[/color]
        >
        > at the begining of the loop add,
        > "srand(clock()) ;"[/color]

        This advice is a bad, bad idea regarding randomness. srand should be called
        only once(!!) in a program and NEVER inside a loop. The reason is that
        calling srand once will yield in a start seed that defines the sequence of
        random numbers produced. After calling rand() the internal seed is updated
        so that the next call or rand() will result in the next random number of
        this specific sequence. If you call srand() more often you are going to jump
        from sequence to sequence which will certainly destroy randomness. Although
        for this case it might not be a real problem you should refrain from
        multiple calls to srand, unless you know exactly what you are doing and what
        the results are.

        Regards
        Chris


        Comment

        • Martijn Lievaart

          #5
          Re: random number geneator

          On Thu, 08 Jan 2004 05:11:44 +0000, Bruce wrote:
          [color=blue]
          > In comp.lang.c++
          > "Sonoman" <sonoman@aol.co m> wrote:
          >[color=green]
          >>get too fancy with the random number generator, but is there a way of
          >>stopping rand() from resetting every time it starts the loop? What other
          >>choices do I have? Can I somehow use the system time to help me get more
          >>random?[/color]
          >
          > Yes, add ONE call like this:
          >
          > srand(time(NULL ));
          >
          > and your done.[/color]

          Sound advice, but be aware that time() has low granularity. You may want
          to mix in some (platform specific) other elements, like the PID of your
          program, or hash the output from netstat and add that. If your program is
          designed to run very shortly, this becomes important.

          I once was bitten by repeated runs of a program giving exactly the same
          results as time() changed only once a second and typically several runs of
          the program could be completed in that one second.

          Also be aware that many rand() implementations are very poor. For a
          typical game this may be acceptable, but for cryptographic operations one
          should use some better pseudo random generator (PRNG).

          If you really need better random numbers, also have a look at EGD.

          HTH,
          M4

          Comment

          • MPBroida

            #6
            Re: random number geneator

            Chris Theis wrote:[color=blue]
            >
            > "Pierre Espenan" <whistling_rabb it@yahoo.com> wrote in message
            > news:zi6Lb.4460 4$Pg1.28552@new sread1.news.pas .earthlink.net. ..[color=green]
            > >[/color]
            > [SNIP][color=green][color=darkred]
            > > >
            > > > for (j = 1; j <= tests; j++)
            > > > {
            > > > while (match != 1)[/color]
            > >
            > > at the begining of the loop add,
            > > "srand(clock()) ;"[/color]
            >
            > This advice is a bad, bad idea regarding randomness. srand should be called
            > only once(!!) in a program and NEVER inside a loop. The reason is that
            > calling srand once will yield in a start seed that defines the sequence of
            > random numbers produced. After calling rand() the internal seed is updated
            > so that the next call or rand() will result in the next random number of
            > this specific sequence. If you call srand() more often you are going to jump
            > from sequence to sequence which will certainly destroy randomness. Although
            > for this case it might not be a real problem you should refrain from
            > multiple calls to srand, unless you know exactly what you are doing and what
            > the results are.[/color]

            In addition, calling srand() only once allows you to replace
            the "clock()" call with some constant so you can get REPEATABLE
            sequences of random number for testing purposes. AFTER testing
            is completed, restore the clock() call (or something similar).

            Mike

            Comment

            • Pierre Espenan

              #7
              Re: random number geneator


              "Chris Theis" <Christian.Thei s@nospam.cern.c h> wrote in message
              news:yv9Lb.1183 4$Tz1.2967@news .chello.at...[color=blue]
              >
              > "Pierre Espenan" <whistling_rabb it@yahoo.com> wrote in message
              > news:zi6Lb.4460 4$Pg1.28552@new sread1.news.pas .earthlink.net. ..[color=green]
              > >[/color]
              > [SNIP][color=green][color=darkred]
              > > >
              > > > for (j = 1; j <= tests; j++)
              > > > {
              > > > while (match != 1)[/color]
              > >
              > > at the begining of the loop add,
              > > "srand(clock()) ;"[/color]
              >
              > This advice is a bad, bad idea regarding randomness. srand should be[/color]
              called[color=blue]
              > only once(!!) in a program and NEVER inside a loop. The reason is that
              > calling srand once will yield in a start seed that defines the sequence of
              > random numbers produced. After calling rand() the internal seed is updated
              > so that the next call or rand() will result in the next random number of
              > this specific sequence. If you call srand() more often you are going to[/color]
              jump[color=blue]
              > from sequence to sequence which will certainly destroy randomness.[/color]
              Although[color=blue]
              > for this case it might not be a real problem you should refrain from
              > multiple calls to srand, unless you know exactly what you are doing and[/color]
              what[color=blue]
              > the results are.[/color]

              Did you actually try to compile and run the code listed? Did you see what
              he is talking about? The same number is generated by rand() in each loop
              because the loop has a constant relationship to clock(). Puting in
              cin.get() into the loop insures that a different seed is generated each
              loop. I ran the modified program for 100 loops and the average for count
              came out to 24 which is very close to the average expected by strict
              mathematical analysis. The reason srand(clock()) is discouraged within a
              loop is that unless special care is taken a new seed will not be generated.
              Run the following code and see what happens...

              #include <iostream>
              #include <ctime>
              #include <bitset>

              using namespace std;

              int main(){
              bitset<365> date;
              while(1){
              srand(clock());
              for(int count=1,i=0; ; ++count){
              i=rand()%365;
              if(date.test(i) ) break;
              date.flip(i);
              }
              cout << count << '\n';
              date.reset();
              cin.get();
              }
              return 0;
              }



              [color=blue]
              >
              > Regards
              > Chris
              >
              >[/color]




              ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
              http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
              ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

              Comment

              • Karl Heinz Buchegger

                #8
                Re: random number geneator

                Pierre Espenan wrote:[color=blue]
                >
                > "Chris Theis" <Christian.Thei s@nospam.cern.c h> wrote in message
                > news:yv9Lb.1183 4$Tz1.2967@news .chello.at...[color=green]
                > >
                > > "Pierre Espenan" <whistling_rabb it@yahoo.com> wrote in message
                > > news:zi6Lb.4460 4$Pg1.28552@new sread1.news.pas .earthlink.net. ..[color=darkred]
                > > >[/color]
                > > [SNIP][color=darkred]
                > > > >
                > > > > for (j = 1; j <= tests; j++)
                > > > > {
                > > > > while (match != 1)
                > > >
                > > > at the begining of the loop add,
                > > > "srand(clock()) ;"[/color]
                > >
                > > This advice is a bad, bad idea regarding randomness. srand should be[/color]
                > called[color=green]
                > > only once(!!) in a program and NEVER inside a loop. The reason is that
                > > calling srand once will yield in a start seed that defines the sequence of
                > > random numbers produced. After calling rand() the internal seed is updated
                > > so that the next call or rand() will result in the next random number of
                > > this specific sequence. If you call srand() more often you are going to[/color]
                > jump[color=green]
                > > from sequence to sequence which will certainly destroy randomness.[/color]
                > Although[color=green]
                > > for this case it might not be a real problem you should refrain from
                > > multiple calls to srand, unless you know exactly what you are doing and[/color]
                > what[color=green]
                > > the results are.[/color]
                >
                > Did you actually try to compile and run the code listed?[/color]

                Did you ever read the documentation for rand()?
                If you call rand(), it will also update the seed value. Thus when calling
                rand() the next time, it will generate a different random number (and
                update the seed value again) such that when calling rand() the next time
                a different random value is generated (and the seed is updated again) etc.

                It is important to let rand() do this in its own, because randomness in
                a computer is a statistical property. If you reseed the random number
                generator to often, this statistical property cannot build up (as with
                all statistics, it works only for a sufficient large number of samples,
                in this case a sufficient large number of random values).
                [color=blue]
                > Did you see what
                > he is talking about? The same number is generated by rand() in each loop
                > because the loop has a constant relationship to clock().[/color]

                It doesn't matter. The main problem is, that srand() should be called only
                once (!) in a program, not often, especially not before every call to rand()!
                BTW: The original code didn't have a call to srand() at all. Thus there could
                not have been any realtionship to clock() (which was also not in the original
                code).

                Common practice is to not use clock() for seeding the random number generator
                but using time() to do that.
                [color=blue]
                > Puting in
                > cin.get() into the loop insures that a different seed is generated each
                > loop.[/color]

                And how does this help in a program which should run on it's own without
                a user?
                [color=blue]
                > I ran the modified program for 100 loops and the average for count
                > came out to 24 which is very close to the average expected by strict
                > mathematical analysis. The reason srand(clock()) is discouraged within a
                > loop is that unless special care is taken a new seed will not be generated.[/color]

                The reason why you shouldn't call srand() more then once is, that you destroy
                the randomness of the sequence if you do it. A random number generator is
                designed to give a *sequence* of random numbers. It is only the sequence
                which shows the properties of randomness. Don't interfere with the
                generation of the sequence or you will destroy randomness very easily.

                --
                Karl Heinz Buchegger
                kbuchegg@gascad .at

                Comment

                • Pierre Espenan

                  #9
                  Re: random number geneator


                  "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
                  news:3FFE71D6.B DDB1187@gascad. at...[color=blue]
                  > Pierre Espenan wrote:[color=green]
                  > >
                  > > "Chris Theis" <Christian.Thei s@nospam.cern.c h> wrote in message
                  > > news:yv9Lb.1183 4$Tz1.2967@news .chello.at...[color=darkred]
                  > > >
                  > > > "Pierre Espenan" <whistling_rabb it@yahoo.com> wrote in message
                  > > > news:zi6Lb.4460 4$Pg1.28552@new sread1.news.pas .earthlink.net. ..
                  > > > >
                  > > > [SNIP]
                  > > > > >
                  > > > > > for (j = 1; j <= tests; j++)
                  > > > > > {
                  > > > > > while (match != 1)
                  > > > >
                  > > > > at the begining of the loop add,
                  > > > > "srand(clock()) ;"
                  > > >
                  > > > This advice is a bad, bad idea regarding randomness. srand should be[/color]
                  > > called[color=darkred]
                  > > > only once(!!) in a program and NEVER inside a loop. The reason is that
                  > > > calling srand once will yield in a start seed that defines the[/color][/color][/color]
                  sequence of[color=blue][color=green][color=darkred]
                  > > > random numbers produced. After calling rand() the internal seed is[/color][/color][/color]
                  updated[color=blue][color=green][color=darkred]
                  > > > so that the next call or rand() will result in the next random number[/color][/color][/color]
                  of[color=blue][color=green][color=darkred]
                  > > > this specific sequence. If you call srand() more often you are going[/color][/color][/color]
                  to[color=blue][color=green]
                  > > jump[color=darkred]
                  > > > from sequence to sequence which will certainly destroy randomness.[/color]
                  > > Although[color=darkred]
                  > > > for this case it might not be a real problem you should refrain from
                  > > > multiple calls to srand, unless you know exactly what you are doing[/color][/color][/color]
                  and[color=blue][color=green]
                  > > what[color=darkred]
                  > > > the results are.[/color]
                  > >
                  > > Did you actually try to compile and run the code listed?[/color]
                  >
                  > Did you ever read the documentation for rand()?
                  > If you call rand(), it will also update the seed value. Thus when calling
                  > rand() the next time, it will generate a different random number (and
                  > update the seed value again) such that when calling rand() the next time
                  > a different random value is generated (and the seed is updated again) etc.
                  >
                  > It is important to let rand() do this in its own, because randomness in
                  > a computer is a statistical property. If you reseed the random number
                  > generator to often, this statistical property cannot build up (as with
                  > all statistics, it works only for a sufficient large number of samples,
                  > in this case a sufficient large number of random values).
                  >[color=green]
                  > > Did you see what
                  > > he is talking about? The same number is generated by rand() in each[/color][/color]
                  loop[color=blue][color=green]
                  > > because the loop has a constant relationship to clock().[/color]
                  >
                  > It doesn't matter. The main problem is, that srand() should be called only
                  > once (!) in a program, not often, especially not before every call to[/color]
                  rand()![color=blue]
                  > BTW: The original code didn't have a call to srand() at all. Thus there[/color]
                  could[color=blue]
                  > not have been any realtionship to clock() (which was also not in the[/color]
                  original[color=blue]
                  > code).
                  >
                  > Common practice is to not use clock() for seeding the random number[/color]
                  generator[color=blue]
                  > but using time() to do that.[/color]

                  Oops! I tried srand(time(NULL )) outside of the loop and this does yield the
                  correct result without cin.get() so I guess this is the source of all the
                  problems
                  generating (pseudo) random numbers I have been having. I just confused
                  clock()
                  with time().
                  [color=blue]
                  >[color=green]
                  > > Puting in
                  > > cin.get() into the loop insures that a different seed is generated each
                  > > loop.[/color]
                  >
                  > And how does this help in a program which should run on it's own without
                  > a user?
                  >[color=green]
                  > > I ran the modified program for 100 loops and the average for count
                  > > came out to 24 which is very close to the average expected by strict
                  > > mathematical analysis. The reason srand(clock()) is discouraged within[/color][/color]
                  a[color=blue][color=green]
                  > > loop is that unless special care is taken a new seed will not be[/color][/color]
                  generated.[color=blue]
                  >
                  > The reason why you shouldn't call srand() more then once is, that you[/color]
                  destroy[color=blue]
                  > the randomness of the sequence if you do it. A random number generator is
                  > designed to give a *sequence* of random numbers. It is only the sequence
                  > which shows the properties of randomness. Don't interfere with the
                  > generation of the sequence or you will destroy randomness very easily.
                  >
                  > --
                  > Karl Heinz Buchegger
                  > kbuchegg@gascad .at[/color]




                  ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
                  http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
                  ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

                  Comment

                  • Sonoman

                    #10
                    Thank you all for the help

                    First of all, Thanks for the help. The srand works better outside the loop.
                    The first time I ran the program I got a sequence of random numbers. BUT on
                    the second time and the times after that, when I execute the program I get
                    the same exact sequence of numbers. Does anyone care to explain why? Any
                    simple solutions out there? Any clever solutions for my print statement? It
                    just looks to stupid. Here is my current code:

                    #include <iostream>
                    #include <cstdlib>
                    #include <ctime>

                    using namespace std;

                    int main()
                    {
                    int index;
                    int i;
                    double j;
                    int match = 0;
                    int days[366] = {0};
                    double count = 0;
                    double sum = 0;
                    double average;

                    srand(clock());

                    for (j = 1; j <= 10000; j++)
                    {
                    while (match != 1)
                    {
                    index = (rand() % 365) + 1;

                    if (days[index] != 1)
                    {
                    days[index]++;
                    count++;
                    }
                    else
                    {
                    for (i = 0; i < 366; i++)
                    {
                    days[i] = 0;
                    }

                    match = 1;
                    sum = sum + count;
                    average = sum/j;
                    }
                    }

                    if( j == 1 || j == 2 || j == 3 || j == 4 || j == 5 ||
                    j == 10 || j == 25 || j == 50 || j == 100 ||
                    j == 1000 || j == 10000)
                    {
                    cout << "test # " << j << " asked " << count <<
                    " persons, the average so far is: " << average << endl;
                    }

                    match = 0;
                    count = 0;
                    }

                    return 0;
                    }


                    Comment

                    • Martijn Lievaart

                      #11
                      Re: Thank you all for the help

                      On Fri, 09 Jan 2004 17:21:36 -0500, Sonoman wrote:
                      [color=blue]
                      > First of all, Thanks for the help. The srand works better outside the loop.
                      > The first time I ran the program I got a sequence of random numbers. BUT on
                      > the second time and the times after that, when I execute the program I get
                      > the same exact sequence of numbers. Does anyone care to explain why? Any
                      > simple solutions out there? Any clever solutions for my print statement? It
                      > just looks to stupid. Here is my current code:
                      >
                      > srand(clock());[/color]

                      CLOCK(3) Linux Programmer's Manual CLOCK(3)

                      NAME
                      clock - Determine processor time

                      SYNOPSIS
                      #include <time.h>

                      clock_t clock(void);

                      DESCRIPTION
                      The clock() function returns an approximation of processor time used by
                      the program.

                      Clock() always returns (aproximately) the same number. Use time() instead.

                      HTH,
                      M4

                      Comment

                      Working...