Chapter 3. Adding Randomness and Probability

In this chapter, we will introduce randomness and probability techniques that can be used to add randomness, chance, and character to AI, which would otherwise be perfect. We will start with a quick "Probability 101" to explain some basic concepts; then, we will demonstrate how to use Stream to control a non-uniform distribution of numbers and use these results to demonstrate probability. We will cover how these are used within Unreal Engine 4. Finally, we will build on top of our enemy AIController to have it randomly perform an action.

This chapter will cover:

  • Probability, probability distribution, and non-uniform distribution
  • Using RandomStream in UE4 to add randomness to our AI
  • Adding random behavior to enemy AI-based states

Introducing probability

We know an event will occur, but how often will it occur? This is how we can quantify probability, and this is what we will use to control the frequency of an outcome. So, let's say we flip a quarter. The event we know will occur is heads (H), but it can still land on tails (T). So, the way we would write the probability of landing on heads is P (H) =?

At this point, we know that heads will occur, although we still don't know how often it will occur. To understand this, we must first get the number of possible outcomes that meet our conditions, which is 1 for heads. Then, we must get the number of events that are equally likely to occur, which is 2. So, now we need to put this in the equation for probability:

# of possible met conditions / # of equality likely outcomes

If we do some basic math and break it down further, we will have 50%:

P (H) = 1/2 = 50%

So what this says is, if you flipped a coin a million or even a billion times, the more the number of times you flip it, the closer it will get to 50% of the events landing equally on heads and on tails. This is useful when controlling the chance that a function will execute.

In our example, we will demonstrate a lottery-type probability. We determined that we have a 20% chance of getting gold, which translates to a decimal of .2. We will generate a random number from 0 through 1. We get .19, and we're in the money! We roll again, and we get .57, we're out of money!

These are some ways to control the execution flow of your AI. You could also apply this to other behaviors that are directly linked to the traits of the specific AI. So, if this character is of a certain class, certain monsters may be more frightened by this class of characters.

Probabilistic distribution

A probabilistic distribution of numbers means that we will know the finite numbers of equal outcomes. So, we would use this if, for example, we had a monster with a lot of weapons. We don't want all of the weapons to drop if the monster dies. Although, if we just applied a simple rule to limit the number of weapons dropped, there is still an equal chance that they could get the rare sword versus the crappy sword that no one wants.

Say we applied probabilistic distribution to the selection of weapons. Here is the scenario. We have 50% items ranked crappy through okay and 35% items ranked well through best, ending with rare items at 15%. Now, we will generate a random number of weapons they can get, and the individual items selected will be generated with probabilistic distribution.

Non-uniform distribution

A non-uniform distribution of numbers means that we will not know the finite number of outcomes. We know that 0 through 1 will be generated but not whether 0 or 1 is more likely to be generated. With this said, this is perfect for creating many unpredictable scenarios. In UE4, we have the typical Random and RandomStream tool. The explicit difference between the two is the fact that we can control the seed that generates our random output.

So, to say it another way, non-uniform distribution is a random generation of numbers that are not equally likely to occur. The true behavior of randomness can be replicated but at the cost of time and efficiency. So, we've come up with algorithms to generate random numbers. One of the tools that UE4 provides to generate random numbers is RandomStream.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset