How does the randomness work?

Can someone please explain the randomness equation a little slower or in a different way?

public float shotsPerSecond = 0.5f;

void Update()
{
    float probability = Time.deltaTime * shotsPerSecond;
    if (Random.value < probability){
        Fire();
    }

I don’t get how shotsPerSecond of 0.5f * Time.deltaTime is retrieving a probability of 80% (at least that is what the instructor said). I do understand Time.deltaTime is creating shots independent of frames.

I do understand Time.deltaTime is creating shots independent of frames.

Actually, it’s a bit different. Shots are created when (Random.value < probability) is evaluated to true.

The variable name probability is probably a bit confusing because it’s not the actual probability.

Update() gets called every frame. According to Unity’s API, Time.deltaTime returns “[t]he time in seconds it took to complete the last frame (Read Only)”. The frame time is not constant.

Random.value returns a “random” float value between 0 and 1.0.


Example:
Frame 1 took 0.25 seconds. 0.25f * 0.5f = 0.125f.
Random.value returns 0.7f.

Frame 2 took 0.12 seconds. 0.12f * 0.5f = 0.06f.
Random.value returns 0.1f.

Frame 3 took 0.32 seconds. 0.32f * 0.5f = 0.16f.
Random.value returns 0.14f.
Fire().


probability is (the time the last frame took for its completation) * (0.5f), hence a fraction of shotsPerSecond. It does not have anything to do with probability since a higher value (below 1.0f) does not make it more or less likely to call Fire(). Call the probability variable whatever you like.

Does that make sense to you?

To find out whether probability has a probability of 80% to be higher than Random.value, add some Debug.Logs in your code to note the values of probability and Random.value for a few frames and calculate the actual probability based on the return value of (Random.value < probability), which is either true or false.

3 Likes

Thank you. This makes sense. The name probability threw me off. It seems like the probability is more in the shotsPerSecond variable. Couldn’t this also have been simplified into if (Random.value < shotsPerSecond) so it truly is more of a probability? This way you know for sure it will be 50% if you put 0.5f for shotsPerSecond.

Indeed, (Random.value < shotsPerSecond) would also be possible for simulating a probability. In the end, it’s not the actual probability, though: probability = event(s) / number of outcome. The event is true or false. The number of outcome is the number of frames.

Time.deltaTime is just used to achieve more pseudo-randomness. However, the variable name shotsPerSecond would be confusing because it rather acts as a threshold in your approach.

1 Like

Does this mean that if my PC is really slow the enemy will fire a lot compared to a fast pc?

Update() is based on frames. Random.value < probability and your keyboard input are checked within Update(). Therefore “a lot” is relative. Neither the player nor the enemies in this project can shoot faster than once per frame, no matter how fast or slow your PC is.

Are these better names for the variables?

shotsPerFrame = 0.5f; ----- every 2 frames a shot
shotsPerSecond = Time.deltaTime * shotsPerFrame; ----- every 2 frames a shot * the time between frames = the shots per second

is my reasoning ok?

public float shotsPerFrame = 0.5f;

void Update()
{

    float shotsPerSecond = Time.deltaTime * shotsPerFrame;
    if (shotsPerSecond > Random.value)
    {
        EnemyFire();
    }
    
}

Well, shotsPerFrame indicates that it is possible to shoot more than one missile during one frame. Unless you changed something, EnemyFire() fires one missile only per call. Since Update() is called once per frame, the maximal missile frequency would be once per frame.

In my opinion, shotsPerFrame makes more sense than probability. However, in the end, the variable name must convey a meaning for you, the developer. :slight_smile:

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms