Random number question

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:

1
2
3
4
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:

1
2
3
4
5
6
7
8
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?
I hopei didn't get your question wrong.
You can do it with keeping assignment out of loop
like;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
time_t t;
time(&t);
srand((unsigned int)t);
int  rTSB(rand() % 100);

while(1) // whatever the loop condition is
{
....
...
..
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
}
}
..
..
..

Last edited on
Thank you, that actually worked!

Although there is one problem with this: my cursor changes to the hourglass, the menu bar disappears and turns white, and when I try to quit, it says that my program is not responding.

This is not that big of a problem to me, but is there a way for my program to run smoothly?
Without the while(1), my program runs smoothly while my pictures are back to being screwed up as before.
Last edited on
while(1) means,always be in loop, an infinite loop.You can give pauses in the loop.(If you really need loop in your code )
If you dont need loop , then dont use it.
Actually,It's difficult to say anything without seeing rest of codes.



Topic archived. No new replies allowed.