Help generating random numbers in MFC
Hi all, I have a function to try and put random numbers into an array. However for each object that I run the function for the same random numbers are being produced. I used time.h to try and seed SRand with that to no avail, I have also tried srand(GetTickCount()); and also GetSystemTime. Please help as this is essential to the rest of my program. Below is the code for the function:
m_XYCoOrds are numbers between 0 and 20.
m_DrawCoOrds are these numbers converted into a form which will be representative when drawn to a window (the y axis are upside down and i wanted a bigger scale)
void CLine::RandomCoOrd()
{
SYSTEMTIME util;
GetSystemTime(&util);
m_XYCoOrds[0][0] = 2; //start point
m_XYCoOrds[0][1] = 18;
m_XYCoOrds[9][0] = 17;//end point
m_XYCoOrds[9][1] = 5;
m_DrawCoOrds[0][0] = 60;
m_DrawCoOrds[0][1] = 60;
m_DrawCoOrds[9][0] = 360;
m_DrawCoOrds[9][1] = 320;
int j = 0; // y axis counter
//srand( util( NULL ) ); // initialize seed "randomly"
srand(GetTickCount());
for(int i=1; i<9; i++)
{
int r = rand() % 21; // generate a random number lower than 20
m_XYCoOrds[i][j] = r;//set the random number as the line coords
r = ((r*20)+20); //convert it to drawable coordinates
m_DrawCoOrds[i][j] = r;
}
j = 1;
for( i=1; i<9; i++)
{
int r = rand() % 21; // generate a random number lower than 20
m_XYCoOrds[i][j] = r;//set the random number as the line coords
r = (((20-r)*20)+20); //convert it to drawable coordinates
m_DrawCoOrds[i][j] = r;
}
}