469,080 Members | 1,731 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,080 developers. It's quick & easy.

problem with rand()

given the following example:

Using gcc, this compiles, runs, and outputs as expected, but on
vc++.net 2003, the first number is always the same despite the time
seed.

#include <ctime>
#include <iostream>

using namespace std;

// output 10 numbers ranging from [0, n)
int main ()
{
srand((unsigned int)time(NULL));

int n = 100;

for (int i = 0; i < 10; ++i)
{
cout << int(double(rand()) * n / (RAND_MAX + 1.0)) << "\n";
}
cout << endl;

return 0;
}

What am i doing wrong?
Charles

Jul 22 '05 #1
6 1967
<ChasW> wrote...
given the following example:

Using gcc, this compiles, runs, and outputs as expected, but on
vc++.net 2003, the first number is always the same despite the time
seed.

#include <ctime>
#include <iostream>

using namespace std;

// output 10 numbers ranging from [0, n)
int main ()
{
srand((unsigned int)time(NULL));

int n = 100;

for (int i = 0; i < 10; ++i)
{
cout << int(double(rand()) * n / (RAND_MAX + 1.0)) << "\n";
}
cout << endl;

return 0;
}

What am i doing wrong?


First, let me say that it is quite possible that on some hardware RAND_MAX
cannot be represented precisely as a double making it impossible to add 1.0
to it with any effect. But that's really not the problem.

You're scaling your numbers and throwing away the lower part, which _does_
differ from run to run even on VC++. Just print out 'rand()' instead.

It seems that the implementation of the pseudo-random number generator in
the C library shipped with VC++ is rather poor. Try finding a better one
on the Web, it shouldn't be that hard.

Victor
Jul 22 '05 #2
You're scaling your numbers and throwing away the lower part, which _does_
differ from run to run even on VC++. Just print out 'rand()' instead.

It seems that the implementation of the pseudo-random number generator in
the C library shipped with VC++ is rather poor. Try finding a better one
on the Web, it shouldn't be that hard.

Victor


I see what you mean. When i print out just the rand(), the first of
ten calls is very similar each time the program is run, but it _is_
different as you say. It almost makes me want to wrap rand() and
discard the first number each time it is called, but I shouldnt have
to do that.

This is really an odd problem for VC++.

It seems to also affect this wrapper function as well, which is taken
from Koenig's Accelerated C++.

I realize this code can be somewhat costly timewise, but it did
provide a simple function for improving number distribution.

int nrand(int n)
{
if (n <= 0 || n > RAND_MAX)
throw domain_error("Argument to nrand is out of range");

const int bucket_size = RAND_MAX / n;
int r;

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

return r;
}

I didnt need to look on the Web, Stroustrup's book has one, but until
now, I havent had to use it.

Cheers,
Charles
Jul 22 '05 #3
ChasW wrote:
I see what you mean. When i print out just the rand(), the first of
ten calls is very similar each time the program is run, but it _is_
different as you say. It almost makes me want to wrap rand() and
discard the first number each time it is called, but I shouldnt have
to do that.

This is really an odd problem for VC++.


I had come to this problem myself, and my quick solution was to omit the
first two calls of rand().

Another system dependent solution is to use a Rand object of .NET. It is
far better than their rand() implementation.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4
>>
This is really an odd problem for VC++.

I had come to this problem myself, and my quick solution was to omit the
first two calls of rand().


My issue was with the first call only, and only after setting a time
seed.

An easy solution is to just call rand() one time after calling
srand(). Think of it as a "warm-up"

Another system dependent solution is to use a Rand object of .NET. It is
far better than their rand() implementation.


Stroustrup's random number generator also is quite suitable for my
needs and is portable.

Charles
Jul 22 '05 #5

<ChasW> wrote in message news:i7********************************@4ax.com...
You're scaling your numbers and throwing away the lower part, which _does_differ from run to run even on VC++. Just print out 'rand()' instead.

It seems that the implementation of the pseudo-random number generator in
the C library shipped with VC++ is rather poor. Try finding a better one
on the Web, it shouldn't be that hard.

Victor


I see what you mean. When i print out just the rand(), the first of
ten calls is very similar each time the program is run, but it _is_
different as you say. It almost makes me want to wrap rand() and
discard the first number each time it is called, but I shouldnt have
to do that.

This is really an odd problem for VC++.


Actually the problem is not odd at all, neither for VC++ nor other rand()
implementations. In general random number generation is a very subtle
problem and many generators suffer from different defects. What you can do
is to use a "warm-up" phase of the generator, which is a common approach
used in some Monte Carlo simulations. Otherwise you could resort to a
"better" generator (without going into details what better means in this
context) like a Mersenne Twister or ran2 from Numerical Recipes. The code
you gave is already an improvement regarding uniformity of the distribution,
but it has runtime drawbacks because its a simple rejection method.

Regards
Chris


Jul 22 '05 #6
ChasW wrote:
This is really an odd problem for VC++.

I had come to this problem myself, and my quick solution was to omit the
first two calls of rand().

My issue was with the first call only, and only after setting a time
seed.

Yes I am talking about the same case, after a call to srand(time(0));

My experiments showed me that after omitting the first two calls of
rand() after srand(), would improve things (and not one call).


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by noone | last post: by
3 posts views Thread by MaKiT | last post: by
6 posts views Thread by Sen-Lung Chen | last post: by
7 posts views Thread by Chris Gordon-Smith | last post: by
7 posts views Thread by Fernando Barsoba | last post: by
8 posts views Thread by =?Utf-8?B?TWlrZSBSYW5k?= | last post: by
10 posts views Thread by Rafael Cunha de Almeida | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.