rand() keeps giving me the same values?

Started by
18 comments, last by Servant of the Lord 11 years, 4 months ago

Hey gamedev

Straight to the point, I'm using rand() to randomise where my enemies spawn each time you play, but for some reason every time I run the game they are always in the same position as they were last time I played. It seems like rand() is using the same seed every time, but I pass time to srand. Any ideas?

CODE! :D

Heres the seeding of srand

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLinw, int nShowCmd)
{
srand(time(0));
HWND hWnd;
WNDCLASSEX wc;
RECT client = {5, 30, CAMERASIZE.right, CAMERASIZE.bottom};
void InitializeD3D(HWND hWnd);
void Pulse();
void KillD3D();
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof WNDCLASSEX;
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hInstance = hInstance;
wc.lpszClassName = L"WINDOW";
wc.lpfnWndProc = WinProc;
wc.style = CS_VREDRAW | CS_HREDRAW;
RegisterClassEx(&wc);
AdjustWindowRectEx(&client, WS_OVERLAPPEDWINDOW, FALSE, NULL);
hWnd = CreateWindowEx(
NULL, L"WINDOW", L"DEAD ROAD",
WS_OVERLAPPEDWINDOW,
client.left, client.top, client.right, client.bottom,
NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nShowCmd);
MSG msg = {0};
InitializeD3D(hWnd);
while (true)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
break;
}
GetCursorPos(&mousePosition);
ScreenToClient(hWnd, &mousePosition);
Pulse();
srand(rand());
}
KillD3D();
return msg.wParam;
}

And here's where I initialize enemy positions, in Zombies constructor

Zombie()
{
animBox.left = 0;
animBox.top = 0;
animBox.right = 23;
animBox.bottom = 42;
position = D3DXVECTOR2((rand() % 4000), (rand() % 4000));
health = 100;
isAlive = true;
width = 23;
height = 42;
animX = 0;
animY = 0;
timePassed = 0;
};

Thanks :)

The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.

Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.

I love the word Clicky
Advertisement

You are calling srand() more than once; you don't need to do "srand(rand());" wink.png

The rule of thumb is, call srand() only once for your entire program, and pass it time(0) (which you did), but you are calling srand() every loop.

See this thread for more details.

That was how I did it originally, the second call to srand was put there to see if it helped fix the problem. It didn't :P

Any other suggestions?

The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.

Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.

I love the word Clicky

Sure, do this:


srand(time(0));
 
for(int i = 0; i < 10; i++)
{
     std::cout << rand() << std::endl;
}

It's a simple sanity check: If each of the 10 values printed are identical, the problem is with rand(). If each (or most) of the 10 values are different, the problem is in your code.

Meanwhile, remove the srand() in the main loop and leave only the srand() at the start of WinMain() and post your complete code exactly as it compiles.

You might try doing a text search for your entire project to make sure that srand() is only called once.

You appear to have some functions pre-declared inside the scope of your WinMain() - is there a reason you're doing that?

Well, Another way to go about this is to save to a file the last generated random number each time you stop the game or generate a random number and use the number as your seed in srand() when you start the game again.

or try time(0)%x where x could be anywhere from 100 to 10000. I dont know but the random algorithm might be working more with the upper values like if time was 1111114756

the next time you run, it could be 1111118908. the srand function could be working with the upper 1's. I dont know. just my suggestion.

That's avoiding the problem, instead of solving the problem. mellow.png

Even if rand() is generating the upper bits as all 1's, that's of no consequence if he's modulating the value:

1111114756 % 4000 = 2756

1111118908 % 4000 = 2908

Besides, "time(0) % x" will generate the exact same numbers until a second passes, then it will generate the exact same numbers until another second passes, and so on.

It was a clever suggestion though, so keep 'em coming! smile.png

I searched through every file for srand and I can confirm that it is only seeded once at the start of main. I made a test console application to test out rand() with the code you provided and it produced 10 (pretty much) different values. So it's obviously a problem with my code, but I'm not sure what the problem is.

[quote name='Jivi' timestamp='1357876563' post='5020142']

Well, Another way to go about this is to save to a file the last generated random number each time you stop the game or generate a random number and use the number as your seed in srand() when you start the game again.

[/quote]

Jivi, I'm not quite sure what you mean. I'm giving it a different a different seed every run through so I don't know what doing that would achieve. But thanks for the suggestion :)

The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.

Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.

I love the word Clicky

Did you search manually, or do you do a Ctrl+F find for "srand" on your project code? Sometimes when we code alot, it's hard to find mistakes that are very obvious mistakes because we are seeing what we expect to see (out of familiarity with the project) instead of what is actually present.

It would be extremely easy to accidentally type "srand()" someplace where you meant to type "rand()" and it would be pretty hard to spot.

Could you upload your code somewhere? If you use DropBox or MediaFire, you could just zip up your project and upload it. Or, just beam it to us by posting the URL.

I can't see you declaring any Zombies in the code you posted, so I assume you do so globally before the WinMain function. If so, then the constructors will be called before srand().

I did a ctrl+f search.

I uploaded the source code here for you. It's probably full of bad practice, but I'm still new to this so I'm trying me best. :) thanks - and let me know if you can spot anything wrong?

The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.

Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.

I love the word Clicky

This topic is closed to new replies.

Advertisement