# Thread: Help generating random numbers in MFC

1. ## 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;

}

}

2. Read up on the srand() function. It can make your rand() function more random.

And in the future, use code tags.

3. ## Sorted

Not sure what he was on about with code tags. If anyone is interested i moved the srand(time) declaration out of the function and this fixed everything.
cheers

4. Yeah, you only want to call srand once in the entire program, almost always at the very beginning. Otherwise srand often gets called over and over only milliseconds apart producing very bad random numbers. And you need code tags and proper tabbing when posting your code on here to make your code much more readable. Take a look at what you have above compared to this and see which you think is easier for us to read:
Code:
```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;

}
}```