Archived

This topic is now archived and is closed to further replies.

Steve-B

Seeding a Random Generator

Recommended Posts

I''ve got a nice randomly generated starfield. This is the code I use to generate the 5000 stars: srand(150); for(i=0; i<5000; i++) { xstar = (rand()%1850)-800; ystar[i] = (rand()%1850)-400; } The problem is they are only random the first time you run the program. After that the stars always appear in the same place. I want the stars to be randomly seeded using the PC clock. I''ve looked through my C manual and found 2 functions called srand (which I''ve used here but doesn''t do much) and randomize (which doesn''t seemingly exist according to VC++). Do I need any special header files to access the randomize function or does it have a different name? Also I tried to use a function called random(); which also doesn''t appear to exist even though it''s listed in my huge C/C++ book. Hope someone can help me get my stars properly randomised.

Share this post


Link to post
Share on other sites
srand gets it''s name because it seeds the random number generator.

You need to call it with something different at the start if you want your randomness to be different each time. Pass it the time.

Share this post


Link to post
Share on other sites
the book im using says to get true randomness you have to have it change over time and suggest you call the time.h function and insert the line

srand (time (NULL))

right before your

rand () % 5000

but it looks like your using a slighly different syntax so i donno if my idea will work.

-----------------------------
Programming is confussing!
AIM: Trebor DoD

Share this post


Link to post
Share on other sites

    
srand(time(NULL)); //you can also use cpu clock cyles i belive

for(i=0; i<5000; i++)
{
xstar = (rand()%1850)-800;
ystar[i] = (rand()%1850)-400;
}


The previous poster was right, I'm just confirming

I'd also like to note you only need to seed it once the entire program, it looks like this might be nested loop that I'm looking at to fill an X and Y, however my brain dosn't feel like analyzing code right now.

---
-Iron Eye
Cyrus Eye design  _//_  My personal site  _//_  Google

"Games usually keep me from making my own..."


[edited by - iron Eye on January 18, 2003 5:12:56 PM]

Share this post


Link to post
Share on other sites
When I had just started learning how to program (in QBASIC), I accomplished this using FileIO. Man, I wish I hadn''t lost all my old, naive QBASIC examples when my hard drive died.

_____________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________

Share this post


Link to post
Share on other sites
Guys thanks a lot for your help! Especially to Digigamer15 and Iron Eye. I tried your solution and it worked like a charm. I now get random stars everytime I run my program. I'll just add one more note in case anyone is confused by my code. When I posted my code it removed the [i] which should come just after the xstar variable. Here is how it should look. Thanks again!


  
srand(time(NULL));

// Setup a Random Starfield

for(i=0; i<5000; i++)
{
xstar[i] = (rand()%1850)-800;
ystar[i] = (rand()%1850)-400;
}


[edited by - Steve-B on January 18, 2003 5:32:31 PM]

[edited by - Steve-B on January 18, 2003 5:38:36 PM]

Share this post


Link to post
Share on other sites