Jump to content
  • Advertisement
Sign in to follow this  
Justindano

Seeding random numbers according to time in SDL

This topic is 2548 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 guys thanks for coming by!
Well ive been stuck up with this problem. My logic is, I want to seed random numbers according to a specific time say 5 seconds..
The algorithm i want is to make a object pop up every 5 seconds but not in random places but in a place where objects exits. I hope this is clear..
Any ideas guys?
Thanks!!

Share this post


Link to post
Share on other sites
Advertisement

Hey guys thanks for coming by!
Well ive been stuck up with this problem. My logic is, I want to seed random numbers according to a specific time say 5 seconds..
The algorithm i want is to make a object pop up every 5 seconds but not in random places but in a place where objects exits. I hope this is clear..
Any ideas guys?
Thanks!!


I don't think you mean you want to "seed" random numbers. A random number seed is a starting point for generating random numbers. Typically, you only want to Seed random numbers once, at the start of your code. The typical method of doing that is to use the current time as the seed. That will help ensure the random numbers generated aren't the same as a previous set.

For your algorithm, assuming you're ucing standard C "rand() calls, I'd call srand(time(NULL)) at the start of your program, and when 5 seconds elapse, generate a random number from 0 to Number of Objects, then pop up an object then.

Like this:

// Assume this hold all the objects that a random object can pop up on
std::vector<TGameObjects> GameObjectVector;

// get the system time, in Milliseconds
int StartTime = timeGetTime();

// seed the random number generator
srand(time(NULL));

while(GameRunning)
{
// Do all your typical Game Updates

// Check if 5 seconds have elapsed
if (timeGetTime() - StartTime >= 5000) {
// Place a new object at a random Game Object's location
// This call generates a random number from 0 to RAND_MAX (typically 32767), then takes the modulo (%) of that number
// of the total number of GameObjects. So, it will be random between 0 and GameObjectVector.size()-1;
int RandomSpot = rand() % GameObjectVector.size();

TGameObject PopupObject(GameObjectVector[RandomSpot].Location);

// initialize PopupObject
// Add to GameObjects list
GameObjectVector.push_back(PopupObject);

// Set StartTime to now so we'll get notified 5 seconds later
StartTime = timeGetTime();
}
}


Is that what you're looking for?

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!