# Methods to spawning an object semi-consistently?

#### Calvert

##### Member
Suppose you want a random/semi-random system that somewhat steadily spawns a type of object.

For example, spawning meteors that a player must deal with in an arcade style game: Meteors should be unpredictable, but generally there shouldn't be too many or too little at a given time.

How would you go about it? I am considering multiple different methods.

#### curato

##### Member
not really, you use the alarm to control how many are on the screen at once and you can use irandom to have the system gice you random positions for them to start so they wouldn't be the same all the time.

#### Calvert

##### Member
not really, you use the alarm to control how many are on the screen at once and you can use irandom to have the system gice you random positions for them to start so they wouldn't be the same all the time.
This is good, but I am considering something less consistent than that.

Suppose you don't want to control how many [objects] are on the screen at once with such sovereignty. Perhaps you would want anywhere from 1 to 3, but without giving yourself the ability to predict which it will be. What then?

#### Roderick

##### Member
In your code that runs when the alarm triggers, randomize the length of the next alarm.

#### curato

##### Member
then just add a random number from 1 to 3 and have a loop create however many you want or you could randomize the alarm interval too.

#### Calvert

##### Member
I think randomizing the alarm interval is the key I was looking for, thanks.

Either way, this topic is still ground for further discussion.

#### FrostyCat

##### Member
Randomizing the alarm interval gives a definite bound on the frequency, and in certain cases this can still look too predictable. If you want to be even less consistent than that, use the properties of expected value on a uniform distribution.

Let P be the number of occurrences you want per unit time divided by the length of a unit of time. Then run this in the Step event:
Code:
``````if (random(1) <= P) {
// Action
}``````
An example of P is 1/room_speed to approximate one occurrence per second.

#### Calvert

##### Member
Randomizing the alarm interval gives a definite bound on the frequency, and in certain cases this can still look too predictable. If you want to be even less consistent than that, use the properties of expected value on a uniform distribution.

Let P be the number of occurrences you want per unit time divided by the length of a unit of time. Then run this in the Step event:
Code:
``````if (random(1) <= P) {
// Action
}``````
An example of P is 1/room_speed to approximate one occurrence per second.
Thanks, this seems to hold a more desirable amount of consistency for me.

This method has the potential to omit the use of alarms, am I correct?

Using your example, how would you set an approximation of [less than] 1 occurrence per second? How would you set approximately 5 occurrences per minute?

#### FrostyCat

##### Member
This method has the potential to omit the use of alarms, am I correct?
The expected value method doesn't need alarms. Alarms are for when you need exact control over the interval.

Using your example, how would you set an approximation of [less than] 1 occurrence per second? How would you set approximately 5 occurrences per minute?
Don't you understand this line?
Code:
``Let P be the number of occurrences you want per unit time divided by the length of a unit of time.``
Your number of occurrences is 5. Your unit of time is 1 minute (i.e. 60*room_speed). Then P is 1/(12*room_speed).

#### curato

##### Member
room_speed is the reference time for one second so just multiply by how many seconds you need.

#### GMWolf

##### aka fel666
Don't you understand this line?
Code:
``Let P be the number of occurrences you``
I was a little confused by that line actually.
Perhaps something clearer would be:
Let P be 1 over the expected time between occurrences in frames.

So if you want 5 per minute, you would expect 60/5 seconds between occurrences, or 12 seconds.
12 seconds is (12 * room_speed) frames.
So P is 1/(12 * room_speed)

#### Calvert

##### Member
The expected value method doesn't need alarms. Alarms are for when you need exact control over the interval.

Don't you understand this line?
Code:
``Let P be the number of occurrences you want per unit time divided by the length of a unit of time.``
Your number of occurrences is 5. Your unit of time is 1 minute (i.e. 60*room_speed). Then P is 1/(12*room_speed).
Thank you. This, along with GMWolf's and curato's explanations make perfect sense.

I understood your line. I didn't understand the code behind your line.