Easy random numbers?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Leif K-Brooks

    Easy random numbers?

    I'm a newbie at C++, but no stranger to other programming languages. I'm
    working on my first C++ program (besides "hello world" and the like),
    and it needs to generate a (pseudo-)random number between 1 and 10. What
    would be the simplest way to do that? All of the libraries I found on
    Google seemed to be made for rocket scientists.


  • Bruce

    #2
    Re: Easy random numbers?

    In comp.lang.c++
    Leif K-Brooks <eurleif.NoSpam Please@ec.REMOV E.ritters.biz> wrote:
    [color=blue]
    >I'm a newbie at C++, but no stranger to other programming languages. I'm
    >working on my first C++ program (besides "hello world" and the like),
    >and it needs to generate a (pseudo-)random number between 1 and 10. What
    >would be the simplest way to do that? All of the libraries I found on
    >Google seemed to be made for rocket scientists.[/color]

    Why not use the standard function in every C++ compiler called, rand()?

    Comment

    • Leif K-Brooks

      #3
      Re: Easy random numbers?

      Bruce wrote:
      [color=blue]
      > Why not use the standard function in every C++ compiler called, rand()?[/color]

      Thanks, that's just what I was looking for.


      Comment

      • David Gausebeck

        #4
        Re: Easy random numbers?

        >> Why not use the standard function in every C++ compiler called, rand()?[color=blue]
        >
        >Thanks, that's just what I was looking for.[/color]

        note that you should call srand() first, to seed the RNG. The typical
        (not especially good) method is

        srand(time(NULL ));

        On *nix, this is somewhat better:

        srand(time(NULL ) ^ getpid());

        -Dave

        Comment

        • Jon Bell

          #5
          Re: Easy random numbers?

          In article <Ufc7b.2169$6Q3 .154@fe01.atl2. webusenet.com>,
          Leif K-Brooks <eurleif.NoSpam Please@ec.REMOV E.ritters.biz> wrote:[color=blue]
          >and it needs to generate a (pseudo-)random number between 1 and 10. What
          >would be the simplest way to do that?[/color]

          The simplest way is to use the functions in the C++ standard library:

          #include <iostream>
          #include <cstdlib>

          using namespace std;

          int main ()
          {
          const int seed = 29325; // or anything else you like
          const int maxrand = 10;
          srand (seed);
          for (int k = 0; k < 20; ++k)
          {
          int x = (rand() % maxrand) + 1;
          cout << x << endl;
          }
          return 0;
          }

          Note that this may not give you very good random numbers, depending on the
          implementation that comes with your compiler. For simple uses it will
          probably suffice, but for sophisticated Monte-Carlo simulations where
          quality of randomness is important, you should upgrade to something better
          before you do serious analysis.

          When I was a graduate student, one of my friends started getting results
          that seemed to indicate the existence of a previously-unknown elementary
          particle. After two weeks of checking, he found out it was a flaw in his
          random number generator. No Nobel Prize for him! :-(


          --
          Jon Bell <jtbellap8@pres by.edu> Presbyterian College
          Dept. of Physics and Computer Science Clinton, South Carolina USA

          Comment

          • Andrew Koenig

            #6
            Re: Easy random numbers?

            Jon> int main ()
            Jon> {
            Jon> const int seed = 29325; // or anything else you like
            Jon> const int maxrand = 10;
            Jon> srand (seed);
            Jon> for (int k = 0; k < 20; ++k)
            Jon> {
            Jon> int x = (rand() % maxrand) + 1;
            Jon> cout << x << endl;
            Jon> }
            Jon> return 0;
            Jon> }

            Jon> Note that this may not give you very good random numbers,
            Jon> depending on the implementation that comes with your compiler.

            I can make a stronger statement than that -- in general, this technique
            *will* not give you very good random numbers, regardless how good the
            implementation is that comes with your compiler. Moreover, the larger
            maxrand is, the worse the random numbers will be.

            Here's why. The built-in rand function is not required to yield random
            numbers larger than 32767. Suppose, for the sake of argument, that this
            particular implementation does limit its random numbers to 0<=n<=32767,
            and that instead of maxrand being 10, it is 10,000. Then:

            if rand() yields then x will be

            0 <= rand() < 10000 0
            10000 <= rand() <= 20000 1
            20000 <= rand() <= 30000 2
            30000 <= rand() <= 32767 0

            You will see that 0 will show up substantially more often than 1 or 2.

            Here is an implementation of a function that does a better job:

            // return a random integer r such that 0 <= r < n.
            int nrand(int n)
            {
            assert (n <= 0 || n > RAND_MAX);

            const int bucket_size = RAND_MAX / n;
            int r;

            do r = rand() / bucket_size;
            while (r >= n);

            return r;
            }

            For more information, please see page 135 in ``Accelerated C++.''


            --
            Andrew Koenig, ark@acm.org

            Comment

            • Rob Williscroft

              #7
              Re: Easy random numbers?

              Andrew Koenig wrote in news:yu99n0dep2 2e.fsf@tinker.r esearch.att.com :
              [color=blue]
              >Jon> for (int k = 0; k < 20; ++k)
              >Jon> {
              >Jon> int x = (rand() % maxrand) + 1;
              >Jon> cout << x << endl;
              >Jon> }
              >Jon> return 0;
              >Jon> }
              >
              >Jon> Note that this may not give you very good random numbers,
              >Jon> depending on the implementation that comes with your compiler.
              >
              > I can make a stronger statement than that -- in general, this
              > technique *will* not give you very good random numbers, regardless how
              > good the implementation is that comes with your compiler. Moreover,
              > the larger maxrand is, the worse the random numbers will be.
              >
              > Here's why. The built-in rand function is not required to yield
              > random numbers larger than 32767. Suppose, for the sake of argument,
              > that this particular implementation does limit its random numbers to
              > 0<=n<=32767, and that instead of maxrand being 10, it is 10,000.
              > Then:
              >
              > if rand() yields then x will be
              >
              > 0 <= rand() < 10000 0
              > 10000 <= rand() <= 20000 1
              > 20000 <= rand() <= 30000 2
              > 30000 <= rand() <= 32767 0
              >
              > You will see that 0 will show up substantially more often than 1 or 2.
              >[/color]

              I think you mixed up you're tables for operator / with those for
              operator %.

              equation is x = (rand() % maxrand) + 1;
              maxrand is 10000.

              if rand() yields then x will be

              0 <= rand() < 10000 rand() + 1
              10000 <= rand() <= 20000 rand() - 10000 + 1
              20000 <= rand() <= 30000 rand() - 20000 + 1
              30000 <= rand() <= 32767 rand() - 30000 + 1

              the 4th set gives a range of [1, 2768] all the others give a range
              of [1, 10000].

              This lead's to values in the range [1, 2768] being more likely
              than values in the range [2769, 10000].

              probability in [1, 2768] = 4 / 32768.0 and
              probability in [2769, 10000] = 3 / 32768.0

              If maxrange = 10 then we have (RAND_MAX) % 10 == 7 and
              (RAND_MAX / 10 == 3276 so we get:

              probability in [1, 8] = (3276 + 1) / 32768.0 and
              probability in [9, 10] = 3276 / 32768.0

              Rob.
              --

              Comment

              • Andrew Koenig

                #8
                Re: Easy random numbers?

                Rob> I think you mixed up you're tables for operator / with those for
                Rob> operator %.

                You're quite right. However, the conclusion is still correct: Using
                rand()%n to compute random numbers in the range [0, n) usually gives a
                nonuniform distribution, and the distribution becomes less uniform as
                n increases.

                --
                Andrew Koenig, ark@acm.org

                Comment

                Working...