Public Group

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

## 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 on other sites

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?

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633715
• Total Posts
3013496
×