problem with rand()

From: ChasW (no email)
Date: 12/29/04


Date: Tue, 28 Dec 2004 20:35:55 -0500

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



Relevant Pages