Jump to content

  • Log In with Google      Sign In   
  • Create Account

rand() keeps giving me the same values?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
19 replies to this topic

#1 littletray26   Members   -  Reputation: 267

Like
0Likes
Like

Posted 10 January 2013 - 09:35 PM

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

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

 

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

 

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

 

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

Sponsor:

#2 Servant of the Lord   Crossbones+   -  Reputation: 21211

Like
1Likes
Like

Posted 10 January 2013 - 09:40 PM

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, 10 January 2013 - 09:44 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#3 littletray26   Members   -  Reputation: 267

Like
0Likes
Like

Posted 10 January 2013 - 09:44 PM

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

#4 Servant of the Lord   Crossbones+   -  Reputation: 21211

Like
0Likes
Like

Posted 10 January 2013 - 09:52 PM

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, 10 January 2013 - 09:54 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#5 Jivi   Members   -  Reputation: 276

Like
0Likes
Like

Posted 10 January 2013 - 09:56 PM

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.



#6 Servant of the Lord   Crossbones+   -  Reputation: 21211

Like
1Likes
Like

Posted 10 January 2013 - 10:01 PM

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, 10 January 2013 - 10:18 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#7 littletray26   Members   -  Reputation: 267

Like
0Likes
Like

Posted 10 January 2013 - 10:05 PM

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.

 

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.

 

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

#8 Servant of the Lord   Crossbones+   -  Reputation: 21211

Like
0Likes
Like

Posted 10 January 2013 - 10:17 PM

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.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#9 LennyLen   Crossbones+   -  Reputation: 4035

Like
2Likes
Like

Posted 10 January 2013 - 10:21 PM

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

#10 littletray26   Members   -  Reputation: 267

Like
0Likes
Like

Posted 10 January 2013 - 10:22 PM

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

#11 littletray26   Members   -  Reputation: 267

Like
0Likes
Like

Posted 10 January 2013 - 10:24 PM

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


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

#12 Jivi   Members   -  Reputation: 276

Like
0Likes
Like

Posted 10 January 2013 - 10:24 PM

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.



#13 LennyLen   Crossbones+   -  Reputation: 4035

Like
0Likes
Like

Posted 10 January 2013 - 10:30 PM

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.

#14 littletray26   Members   -  Reputation: 267

Like
0Likes
Like

Posted 10 January 2013 - 10:33 PM

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


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

#15 LennyLen   Crossbones+   -  Reputation: 4035

Like
2Likes
Like

Posted 10 January 2013 - 10:39 PM



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.

#16 Servant of the Lord   Crossbones+   -  Reputation: 21211

Like
0Likes
Like

Posted 10 January 2013 - 10:46 PM

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


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#17 littletray26   Members   -  Reputation: 267

Like
0Likes
Like

Posted 10 January 2013 - 10:50 PM

Okay, thank you so much everyone :)


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

#18 littletray26   Members   -  Reputation: 267

Like
0Likes
Like

Posted 10 January 2013 - 10:51 PM

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


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

#19 LennyLen   Crossbones+   -  Reputation: 4035

Like
0Likes
Like

Posted 10 January 2013 - 11:18 PM

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.

#20 Servant of the Lord   Crossbones+   -  Reputation: 21211

Like
1Likes
Like

Posted 10 January 2013 - 11:40 PM

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.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS