• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
littletray26

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

0

Share this post


Link to post
Share on other sites

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
1

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?

0

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
0

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.

0

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
1

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

0

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.

0

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

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?

0

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

0

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.

0

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

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

0

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

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

0

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0