Followers 0

# rand() keeps giving me the same values?

## 19 posts in this topic

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.hInstance = hInstance;
wc.lpszClassName = L"WINDOW";
wc.lpfnWndProc = WinProc;
wc.style = CS_VREDRAW | CS_HREDRAW;

RegisterClassEx(&wc);

hWnd = CreateWindowEx(
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 :)

0

##### Share on other sites

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

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
1

##### 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?

0

##### 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
0

##### 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.

0

##### Share on other sites

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

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!

Edited by Servant of the Lord
1

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

0

##### 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.

0

##### 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().
2

##### 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?

0

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

0

##### 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.

0

##### 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.
0

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

0

##### 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?

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.
2

##### Share on other sites

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

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

0

##### Share on other sites

Okay, thank you so much everyone :)

0

##### Share on other sites

So what can I do to resolve the issue? What are the alternatives to using Globals?

0

##### 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.
0

##### Share on other sites

Better yet, use a std::vector, and push_back() the Zombies. If you have to, std::vector could be global, but your push_backs() need to be inside a function, so the Zombies are constructed from within the functions after srand() is called.

1

## Create an account

Register a new account