• Advertisement
Sign in to follow this  

rand() keeps giving me the same values?

This topic is 1836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
[code] 

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;
}
[/code]

 

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

 

[code] 

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;
};
[/code]

 

Thanks :)

Share this post


Link to post
Share on other sites
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.

Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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?

Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

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 :)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
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().

 

The code posted isn't the full code, it's just what I thought was relevant. the full source was just uploaded in the previous post if you'd like to have a look :)

Share this post


Link to post
Share on other sites

try putting srand(time(0)) where you place srand(rand()) or before you generate the position data to test check if it changes at all. Also, check your position update function for any code that might be doing something you dont want.

Share this post


Link to post
Share on other sites
the full source was just uploaded in the previous post if you'd like to have a look

That filehost is useless. Each time I click the link it emails me it takes me to a page asking me what file I want to download so that they can email me a link to it.

Share this post


Link to post
Share on other sites
the full source was just uploaded in the previous post if you'd like to have a look

That filehost is useless. Each time I click the link it emails me it takes me to a page asking me what file I want to download so that they can email me a link to it.

 

I tried the link myself and everything seemed to work fine. What hosting site would you prefer? :)

Share this post


Link to post
Share on other sites


the full source was just uploaded in the previous post if you'd like to have a look


That filehost is useless. Each time I click the link it emails me it takes me to a page asking me what file I want to download so that they can email me a link to it.


 
I tried the link myself and everything seemed to work fine. What hosting site would you prefer? smile.png
 



It turns out their site just doesn't like OPera, it worked for me in IE.

And yes, you do declare your zombies globally in GameSkel.cpp, so their position is being set before your WinMain function is even started.

Share this post


Link to post
Share on other sites

That webhost also doesn't like Google Chrome (and also refuses @mailinator.com addresses huh.png).

 

But I'm glad the problem was resolved, thanks Lenny! 

 

Classes in the global scope get constructed before code enters the main() (or WinMain()) function. Really, globals should be avoided, but it's understandable for small programs - you just have to be very aware of the construction-order problems (there's no garuntee in what order the classes will be constructed if they are in the global scope, but they are guaranteed to be constructed before entering the main entry point function).

Share this post


Link to post
Share on other sites
You can declare them inside your WinMain() function, which will require you to then pass the array to any functions that need to access the Zombies.

If you want to minimize code changes, you can change your declaration of test to a pointer to a Zombie, and then inside your WinMain function, use new[] to allocate the space.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement