Okay, I've been able to narrow down the problem to a certain point in a function called when the user presses the Generate Button (I've included a screenshot of my program). Here's the function in its entirety. And I've commented the lines that I was able to narrow it down to.
Code:
void GenerateNumbers(HWND hwnd)
{
int index = 0;
int index2 = 0;
int result;
int number = GetDlgItemInt(hwnd, IDC_INPUT_NUMBER, NULL, FALSE);
int min = GetDlgItemInt(hwnd, IDC_INPUT_MINIMUM, NULL, FALSE);
int max = GetDlgItemInt(hwnd, IDC_INPUT_MAXIMUM, NULL, FALSE);
int* generated;
generated = GlobalAlloc(GPTR, number);
if(generated == NULL)
MessageBox(hwnd, "Failure allocating generated", "Error:", MB_ICONERROR | MB_OK);
int outputsize = log10(max) + 2;
char* output;
output = GlobalAlloc(GPTR, outputsize);
if(output == NULL)
MessageBox(hwnd, "Failure allocating output", "Error:", MB_ICONERROR | MB_OK);
InitCharArray(outputsize, output, 0);
for(index = 0; index < number; index = index)
{
time(¤ttime);
seed *= (unsigned int)currenttime + seed + index + 1;
if(seed == 0)
{
seed += index + 1;
}
srand((unsigned int)(seed));
result = rand() % (max - min + 1) + min;
if(NoDuplicates == TRUE)
{
for(index2 = 0; index2 < index; ++index2)
{
if(result == generated[index2])
break;
}
}
if(result == generated[index2])
continue;
generated[index] = result;
/*****Between here*****/
itoa(result, output, 10);
/*******and here*******/
SendDlgItemMessage(hwnd, IDC_OUTPUT, LB_ADDSTRING, 0, (LPARAM)output);
++index;
}
GlobalFree(generated);
GlobalFree(output);
}
Basically, when I have the program generate five random numbers, on the first run through, only, the value of generated[index] when index is 4 changes somewhere in the line indicated above. After that, it stays the same when index is 4. But, even though that stays the same, if I keep on pressing Generate, every so often, I will get something similar to the following output from my debug window (I've erased all debug code from the above code).
Code:
generated[index] =
result = 5
generated[index] = 0
output = 53
When the output I would expect would be
Code:
generated[index] = 5
result = 5
generated[index] = 5
output = 53
I don't know if this is simply a glitch that occurs when getting the value to my debug window and the value of generated[index] is actually just fine, But I don't think so, since it only occurs sometimes.
Does anyone have any ideas what's going on?