I am trying to store a single random number from 0 to 99 to a variable.
This is what I used in my code to do that:

time_t t;
time(&t);
srand((unsigned int)t);
int  rTSB(rand() % 100);

I am trying to make a single random picture show up when you start the program.
However, when I minimize and maximize the window, my picture changes, which is not supposed to happen. Also, when I move my window off the screen and back, part of the picture is the old one and the other part that went off the screen became the other picture.

The code for the picture is something like:

case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);

if (rTSB >= 0 && rTSB < 35) {
//code to display picture
} else if (rTSB >= 35 && rTSB < 45) {
//code to display another picture
}

and so on . . .

I am thinking that the program loops and a new number gets stored to rTSB every second.
How I do just store one variable and stop the loop? How do I prevent rTSB from having a different value?

The reason is the code for changing the picture runs at wm_paint. Wm_paint is passed when the windows needs redrawing which happens everytime you minimize, maximize, drag some other application above your app etc.

If you want to select your picture only during once startup, do it at the wm_create event.

Or you could use a roundabout way by using a static integer and increment it when the above code runs once. If the value is above 0, dont run the code again.

case WM_PAINT: 
static int counter;

If (counter == 0 ){
    // your code for selecting the random number here.
    counter++; 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.