rand() function doesn't work well??

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

    rand() function doesn't work well??

    Dear all,

    I try to use rand() to generate some integers;
    but these integers are never larger than 40000.

    I use Borland C++ Builder to test, the result is also same.
    But I use g++ in CYGWIN, the result is perfect.
    What's wrong?

    Here is my test code.
    -------------------------------------------
    #include <cstdlib>
    #include <iostream>
    #include <ctime>
    using namespace std;

    int main()
    {
    srand(time(NULL ));
    int count=0;
    for(;;) {
    int value=rand();
    if (value>40000) {
    cout << value << endl;
    if (count++ >10) {
    break;
    }
    }
    }
    return 0;
    }
    -----------------------------------------


  • dandelion

    #2
    Re: rand() function doesn't work well??


    "cylin" <cylin@avant.co m.tw> wrote in message
    news:2trpliF237 6hfU1@uni-berlin.de...[color=blue]
    > Dear all,
    >
    > I try to use rand() to generate some integers;
    > but these integers are never larger than 40000.
    >
    > I use Borland C++ Builder to test, the result is also same.
    > But I use g++ in CYGWIN, the result is perfect.
    > What's wrong?[/color]

    Knuth, the art of computer programming, Vol III and a mathematical analysis
    of the sources (if provided) will probably tell you.



    Comment

    • Sigurd Stenersen

      #3
      Re: rand() function doesn't work well??

      cylin wrote:[color=blue]
      > Dear all,
      >
      > I try to use rand() to generate some integers;
      > but these integers are never larger than 40000.
      >
      > I use Borland C++ Builder to test, the result is also same.
      > But I use g++ in CYGWIN, the result is perfect.
      > What's wrong?[/color]

      RTFM

      "The rand function returns a pseudorandom integer in the range 0 to
      RAND_MAX"


      --


      Sigurd



      Comment

      • Larry Brasfield

        #4
        Re: rand() function doesn't work well??

        "cylin" <cylin@avant.co m.tw> wrote in message news:2trpliF237 6hfU1@uni-berlin.de...[color=blue]
        > Dear all,
        >
        > I try to use rand() to generate some integers;
        > but these integers are never larger than 40000.
        >
        > I use Borland C++ Builder to test, the result is also same.
        > But I use g++ in CYGWIN, the result is perfect.
        > What's wrong?[/color]

        What's wrong is your expectation that RAND_MAX
        will be greater than 32767. You mistake a result that
        happened to meet your incorrect expectation for
        confirmation that your expectation is good. The fact
        is that an implementation with RAND_MAX no
        greater than 32767 can still conform (perfectly) to
        the standard requirements placed on the C library.

        If you truly need a larger range of random integers,
        either pick a compiler and runtime libary that does
        what you want, or find a pseudo-random number
        generator that does. Many are floating around.
        [color=blue]
        > Here is my test code.[/color]
        [cut because not at issue]

        --
        --Larry Brasfield
        email: donotspam_larry _brasfield@hotm ail.com
        Above views may belong only to me.


        Comment

        • Martin Ambuhl

          #5
          Re: rand() function doesn't work well??

          cylin wrote:[color=blue]
          > Dear all,
          >
          > I try to use rand() to generate some integers;
          > but these integers are never larger than 40000.
          >
          > I use Borland C++ Builder to test, the result is also same.
          > But I use g++ in CYGWIN, the result is perfect.
          > What's wrong?
          >
          > Here is my test code.[/color]

          Please don't post C++ code to comp.lang.c unless you are *sure* that it
          is also C, and that the semantics are the same. These are two different
          languages. In your case, the <cstdlib>, <iostream>, and <ctime> headers
          are not C; the "using" statement is not C; the use of declarations other
          than either at the top of a block or outside of all functions is now C,
          but only for the C99 standard; the use of undeclared variables cout and
          endl, subjected to strange use of the shift left operator is not C.

          Your test condition 'if (value>40000)' is backwards if you mean to never
          output a value greater than 40000. If one compiler produced code
          resulting in output you expected, and the other did not, then you did
          not give them the same input code.

          Since you posted to comp.lang.c, you are entitled to a C version that
          does what you seem to want:

          #include <stdlib.h>
          #include <stdio.h>
          #include <time.h>

          int main(void)
          {
          int value;
          int count;
          srand((unsigned ) time(0));
          for (count = 0, value = rand(); count < 10; value = rand()) {
          if (value > 40000)
          continue;
          printf("%d\n", value);
          count++;
          }
          return 0;
          }

          Comment

          • Martin Ambuhl

            #6
            Re: rand() function doesn't work well??

            cylin wrote:
            [color=blue]
            > Dear all,
            >
            > I try to use rand() to generate some integers;
            > but these integers are never larger than 40000.[/color]

            I see that I misunderstood your problem. I imagined that the "never
            larger than 40000" was part of the specification, rather than the
            problem. rand() returns an int in the range 0 to RAND_MAX, and in C
            RAND_MAX must be at least 32767. For implementations with
            RAND_MAX==32767 (or any other value not greater than 40000), you will
            obviously not see a result greater than 40000. This is all covered in
            your documentation. It is provided for a reason. If you have somehow
            gotten a compiler without its documentation, buy a basic textbook.

            Comment

            • cylin

              #7
              Re: rand() function doesn't work well??


              "Larry Brasfield" <donotspam_larr y_brasfield@hot mail.com> ¼¶¼g©ó¶l¥ó·s»D
              :uF9yncAuEHA.10 48@tk2msftngp13 .phx.gbl...[color=blue]
              > "cylin" <cylin@avant.co m.tw> wrote in message[/color]
              news:2trpliF237 6hfU1@uni-berlin.de...[color=blue][color=green]
              > > Dear all,
              > >
              > > I try to use rand() to generate some integers;
              > > but these integers are never larger than 40000.
              > >
              > > I use Borland C++ Builder to test, the result is also same.
              > > But I use g++ in CYGWIN, the result is perfect.
              > > What's wrong?[/color]
              >
              > What's wrong is your expectation that RAND_MAX
              > will be greater than 32767. You mistake a result that
              > happened to meet your incorrect expectation for
              > confirmation that your expectation is good. The fact
              > is that an implementation with RAND_MAX no
              > greater than 32767 can still conform (perfectly) to
              > the standard requirements placed on the C library.
              >
              > If you truly need a larger range of random integers,
              > either pick a compiler and runtime libary that does
              > what you want, or find a pseudo-random number
              > generator that does. Many are floating around.
              >[/color]

              Yes, thanks.
              [color=blue][color=green]
              > > Here is my test code.[/color]
              > [cut because not at issue]
              >
              > --
              > --Larry Brasfield
              > email: donotspam_larry _brasfield@hotm ail.com
              > Above views may belong only to me.
              >
              >[/color]


              Comment

              • Catalin Pitis

                #8
                Re: rand() function doesn't work well??


                "cylin" <cylin@avant.co m.tw> wrote in message
                news:2trt94F22d n2hU1@uni-berlin.de...[color=blue]
                >
                > "Larry Brasfield" <donotspam_larr y_brasfield@hot mail.com> ¼¶¼g©ó¶l¥ó·s»D
                > :uF9yncAuEHA.10 48@tk2msftngp13 .phx.gbl...[color=green]
                >> "cylin" <cylin@avant.co m.tw> wrote in message[/color]
                > news:2trpliF237 6hfU1@uni-berlin.de...[color=green][color=darkred]
                >> > Dear all,
                >> >
                >> > I try to use rand() to generate some integers;
                >> > but these integers are never larger than 40000.
                >> >
                >> > I use Borland C++ Builder to test, the result is also same.
                >> > But I use g++ in CYGWIN, the result is perfect.
                >> > What's wrong?[/color]
                >>
                >> What's wrong is your expectation that RAND_MAX
                >> will be greater than 32767. You mistake a result that
                >> happened to meet your incorrect expectation for
                >> confirmation that your expectation is good. The fact
                >> is that an implementation with RAND_MAX no
                >> greater than 32767 can still conform (perfectly) to
                >> the standard requirements placed on the C library.
                >>
                >> If you truly need a larger range of random integers,
                >> either pick a compiler and runtime libary that does
                >> what you want, or find a pseudo-random number
                >> generator that does. Many are floating around.
                >>[/color]
                >
                > Yes, thanks.
                >[/color]

                Use the random library from boost:



                Catalin


                Comment

                • Merrill & Michele

                  #9
                  Re: rand() function doesn't work well??

                  [color=blue][color=green]
                  > > cylin wrote:
                  > > Dear all,
                  > >
                  > > I try to use rand() to generate some integers;
                  > > but these integers are never larger than 40000.[/color][/color]
                  [color=blue]
                  > "Martin Ambuhl wrote:
                  > I see that I misunderstood your problem. I imagined that the "never
                  > larger than 40000" was part of the specification, rather than the
                  > problem. rand() returns an int in the range 0 to RAND_MAX, and in C
                  > RAND_MAX must be at least 32767. For implementations with
                  > RAND_MAX==32767 (or any other value not greater than 40000), you will
                  > obviously not see a result greater than 40000. This is all covered in
                  > your documentation. It is provided for a reason. If you have somehow
                  > gotten a compiler without its documentation, buy a basic textbook.[/color]

                  This is a record-setting event in terms of politeness and helpfulness given
                  your crossposting use of that other language. MPJ


                  Comment

                  • Martin Ambuhl

                    #10
                    Re: rand() function doesn't work well??

                    Merrill & Michele wrote:
                    [color=blue][color=green][color=darkred]
                    >>>cylin wrote:
                    >>>Dear all,
                    >>>
                    >>>I try to use rand() to generate some integers;
                    >>>but these integers are never larger than 40000.[/color][/color]
                    >
                    >[color=green]
                    >>"Martin Ambuhl wrote:
                    >>I see that I misunderstood your problem. I imagined that the "never
                    >>larger than 40000" was part of the specification, rather than the
                    >>problem. rand() returns an int in the range 0 to RAND_MAX, and in C
                    >> RAND_MAX must be at least 32767. For implementations with
                    >>RAND_MAX==327 67 (or any other value not greater than 40000), you will
                    >>obviously not see a result greater than 40000. This is all covered in
                    >>your documentation. It is provided for a reason. If you have somehow
                    >>gotten a compiler without its documentation, buy a basic textbook.[/color]
                    >
                    >
                    > This is a record-setting event in terms of politeness and helpfulness given
                    > your crossposting use of that other language. MPJ
                    >[/color]

                    Note that it was not "my" crossposting. The OP crossposted. Since his
                    question proves that he had not checked the newsgroups' FAQs and he is
                    not a known comp.lang.c poster, it would have been presumptuous for me
                    to have done other than to leave his crossposting unchanged.

                    Comment

                    • Ben Pfaff

                      #11
                      Re: rand() function doesn't work well??

                      "Catalin Pitis" <catalin.pitis@ iquestint.com.r enameme> writes:
                      [color=blue]
                      > Use the random library from boost:
                      >
                      > http://www.boost.org/libs/random/index.html[/color]

                      That's a C++ library as far as I can tell. Please don't post C++
                      suggestions to comp.lang.c, even cross-posted.
                      --
                      Ben Pfaff
                      email: blp@cs.stanford .edu
                      web: http://benpfaff.org

                      Comment

                      • Merrill & Michele

                        #12
                        Re: rand() function doesn't work well??

                        > >MPJ wrote:[color=blue][color=green]
                        > > This is a record-setting event in terms of politeness and helpfulness[/color][/color]
                        given[color=blue][color=green]
                        > > your crossposting use of that other language. MPJ
                        > >[/color]
                        >
                        > Note that it was not "my" crossposting. The OP crossposted. Since his
                        > question proves that he had not checked the newsgroups' FAQs and he is
                        > not a known comp.lang.c poster, it would have been presumptuous for me
                        > to have done other than to leave his crossposting unchanged.[/color]

                        'Your' did refer to the OP and was poor English. MPJ


                        Comment

                        • Chris Schumacher

                          #13
                          Re: rand() function doesn't work well??

                          "Larry Brasfield" <donotspam_larr y_brasfield@hot mail.com> wrote in
                          news:uF9yncAuEH A.1048@tk2msftn gp13.phx.gbl:
                          [color=blue]
                          > If you truly need a larger range of random integers,
                          > either pick a compiler and runtime libary that does
                          > what you want, or find a pseudo-random number
                          > generator that does. Many are floating around.[/color]

                          Of course, on the other hand you could simply use the following code;


                          srand( time(0) );
                          b = ( rand() * ( ( rand() % 10 ) + 1 ) );

                          I don't know if that'll give the original poster the range of numbers he
                          needs, but it should help.



                          -==Kensu==-

                          Comment

                          • Larry Brasfield

                            #14
                            Re: rand() function doesn't work well??

                            "Chris Schumacher" <kensu__@hotmai l.com> wrote in message news:Xns958BA94 E5F822kensuhotm ailcom@207.115. 63.158...[color=blue]
                            > "Larry Brasfield" <donotspam_larr y_brasfield@hot mail.com> wrote in
                            > news:uF9yncAuEH A.1048@tk2msftn gp13.phx.gbl:
                            >[color=green]
                            >> If you truly need a larger range of random integers,
                            >> either pick a compiler and runtime libary that does
                            >> what you want, or find a pseudo-random number
                            >> generator that does. Many are floating around.[/color]
                            >
                            > Of course, on the other hand you could simply use the following code;
                            >
                            >
                            > srand( time(0) );
                            > b = ( rand() * ( ( rand() % 10 ) + 1 ) );
                            >
                            > I don't know if that'll give the original poster the range of numbers he
                            > needs, but it should help.[/color]

                            He may be after more than just a greater range.
                            For example, he might have hoped for values
                            within the range to have approximately equal
                            probabilities of being produced. If that common
                            requirement existed, he would be disappointed
                            in your solution. I would point out why that is so,
                            but I think it would be better for you (or anybody
                            else considering rolling their own PRNG on the
                            cheap) to consider the problem on their own. If
                            you can easily see the problem, then consider
                            how readily a more subtle defect could occur.
                            If you cannot see the problem, then I would
                            encourage you to rely on other people's well-
                            tested work in this realm.

                            --
                            --Larry Brasfield
                            email: donotspam_larry _brasfield@hotm ail.com
                            Above views may belong only to me.


                            Comment

                            Working...