Course contentsShow
← Unity Game Development
Lesson 565 of 8288. Artificial IntelligenceFree lesson

Creating Predictable Randomness in Utility Choices

Introduce controlled stochastic elements to utility scores to prevent repetitive behavior and create more unpredictable, lifelike NPC decision-making.

In this lesson, you will learn how to introduce controlled stochastic elements into your utility scoring system to ensure your NPCs don't become predictable robots.

Breaking the Deterministic Loop

In previous lessons, we calculated utility scores using fixed response curves and weighting factors. While this ensures the NPC chooses the "mathematically optimal" action, it creates a sterile, repetitive loop. If an NPC’s hunger score is 0.8 and their exhaustion is 0.7, they will choose "Eat" every single time they update, even if the difference between the two choices is negligible.

To create more lifelike behavior, we inject a layer of utility noise or stochastic variance into our final scores. This allows an NPC to occasionally choose a "sub-optimal" action, simulating human impulsivity or indecision.

Implementing Stochastic Utility

The trick is to add a small random offset to the final utility score before the selector picks the highest value. By multiplying a random value by a "fluctuation factor," you can control how much the AI deviates from its logical plan.

public float CalculateUtilityWithNoise(float baseScore)
{
    // Define how much the AI can deviate from its logical path
    float fluctuationFactor = 0.15f; 
    
    // Generate a random shift between -0.15 and +0.15
    float noise = Random.Range(-fluctuationFactor, fluctuationFactor);
    
    // Return the modified score
    return Mathf.Clamp01(baseScore + noise);
}

By applying this to your UtilitySelector, the NPC won't always pick the absolute highest score. If two actions have very similar utility values, the noise ensures the selection varies. This keeps the agent’s behavior feeling organic, as they might choose to "Rest" even when "Eat" is slightly more urgent, simply because the variance made the "Rest" action appear marginally more attractive in that specific moment.

Balancing Logic and Randomness

You should keep your fluctuationFactor relatively small. If the noise is too high, the NPC’s behavior will feel erratic and nonsensical. If it’s too low, the NPC remains purely robotic. Use this randomness to blur the lines between two closely ranked decisions, not to overturn clear priorities.

Key Takeaway: Injecting small, controlled amounts of randomness into utility scores prevents repetitive, machine-like behavior and allows for more nuanced, unpredictable NPC decision-making.