Seeding random numbers according to time in SDL

Started by
0 comments, last by BeerNutts 12 years, 3 months ago
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!!
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?

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement