Easy random numbers?

L

Leif K-Brooks

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.
 
B

Bruce

In comp.lang.c++
Leif K-Brooks said:
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.

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

David Gausebeck

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

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
 
J

Jon Bell

and it needs to generate a (pseudo-)random number between 1 and 10. What
would be the simplest way to do that?

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! :-(
 
A

Andrew Koenig

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++.''
 
R

Rob Williscroft

Andrew Koenig wrote in
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.

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.
 
A

Andrew Koenig

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top