Hi,
from time to time there are questions like:
How to get random this or random that
Here is the answer (in logic bricks :yes:).
The Random Actuator is what the name says an actuator. That means you can’t use it to measure random events. But - you can use it to create random events.
For a better understanding lets imagine an example: throwing a die
Assumptions
A die has 6 faces with the numbers 1…6. Each number exists exactly once. If the die is thrown each side has the same likelihood to face the sky ;).
How to throw a die in logic?
Yes we could create a cube and throw it around to let the physics decide (as in real live). But this takes quite some time, is processing intensive and just not a practical solution.
We use the Random Actuator
If you look at it it is like a Property Actuator. Rather than putting constant values into a property it puts a random value into a property.
That is all it does.
Why it looks so complicated?
That is because there is more than one way to create random numbers.
First of all you have to tell what format your random number should have:
- boolean (True/False = 0/1)
- integer (natural numbers + negative)
- float (numbers with fractions)
You are missing strings? Strings are no numbers
For our die we need integer (we assume the dice top most face is up = no fractions).
Now we need to choose the distribution.
The distribution tells the Random Actuator how to generate the random number. E.g. each number with equal likelihood or some numbers with higher likelihood etc.
The faces of our die have equal likelihood. So we choose the distribution: Uniform.
In the above picture I concatenated the output format and the distribution to the generator type. Your die gets “Int Uniform”.
What is a seed?
Our computer can not truly create random numbers. The computer “computes” a sequence of pseudo-random numbers. You get a giant sequence of numbers that fulfill the statistical requirements of random numbers. This sequence does not change (within one pseudo-random number generator).
When the end of the sequence is reached it start from beginning again. So it is a loop.
You can tell the generator where you want to start within the loop. You do that by choosing the number to start = seed.
The same seed = the generator creates the same sequence of pseudo-random numbers every time.
You set the start number with the field seed.
This comes quite handy if you want reproducible situations e.g. “random created level” or “simulations”. With the same seed you get the exact same level/simulation.
In most cases you do not want reproducible random numbers. So you can simply start with a true random number. A good example is… the system time. That means the seed depends on the player when the player started the game. You set this with a seed of zero.
The throw
We throw the die by activating the Random Actuator!
You can do that with any sensor/controller you like.
Be aware you get the result AFTER the Random Actuator is executed. That means you should throw the die BEFORE you need the result.
Resetting the throw
If you want to throw the die again you should be able to see if you already have a result or not.
The easiest way is to set the property to a value that can not be created by the Random Actuator. A die can’t show a zero. We choose zero. (Be aware zero is not always the best choice.)
If the property has a zero we interpret it as “no throw”.
If the property has a value of 1…6 we interpret is as “result of the throw is …”.
Everything else should be seen as invalid
With that we can easily take the die from the table, just by setting zero after we evaluated the throw.
Note the True Pulse on the property sensor. This is necessary in case the die is thrown immediately again. The sensor’s state would not change after the next throw and therefore not activate the resetting again.
Make sure the Property Actuator is above the Random actuator. Otherwise the reset can clear out a new throw.
Evaluate the throw
Now we have a die that can be thrown. We get a result in a property. And we can throw the die again.
What to do with that?
You can evaluate this value
By checking the properties value you can trigger any action you like including throwing the die again.
Be aware when using floats you should evaluate intervals rather than fixed numbers. Otherwise it is like throwing a stone into the ocean hoping it hits a fish.
I hope this helps you to understand the Random Actuator a bit more.
If you have comments let me know
Monster