FAQ - How to use the random actuator

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

1 Like

Thank you, I have always been iffy about the random actuator, but now I understand. :slight_smile:

I never use random actuator before, but i got the feeling that this is can be very useful someday. Thank you.
But if you put a simple .blend file, so we can examine the object’s behavior with a random actuator setup on it, it might be even more useful and easier to understand.
Thanks again :yes:

Ok,

a quick demo written in 2.61. The principles are for all versions.

it includes:

  • die throw (via <space>)
  • die reset
  • die value evaluation (1=restart; 6=winner)
  • message handling (notifying a display)
  • property copy (from thrower -> display)

Attachments

RandomActuatorDemo.blend (142 KB)

Wow, you really did it! You make the .blend.
This is great. Thank you very much, Prof :slight_smile:
Another helpful resource from you.

this was very helpfull thanks!

For more advanced uses (like using Python to get your random numbers), you can always do this in a script, something like…


random = bge.logic.getRandomFloat() #returns a float between 0 and 1
randNumber = 1+(random*9) #multiplies it to a float between one and ten
randInteger = int(randNumber) #converts the float to an integer

From my experience, you can run the first function to give an assignment to more than one variable and have a different number for each and the results are truly random.

I know your tutorials are usually oriented around using the bricks, but python can be useful if you’re needing to do something like creating a master system capable of complex logic concepts such as random level generation.

There is no question, with Python you get the random numbers more directly.

The random module alone does not help.
The random actuator alone does not help either.

The intention of the tutorial is to show how to create a context of the random actuator to make it usable at all. I think that this is not obvious especially for beginners.

As easy as it is, Python code is restricted to Python programmers. (You are a Python programmer when you are able to write Python code ;).) I assume everybody who is able to write code to create random numbers is able to write the code for the context too. Currently I do not see the need for a “Random with Python” tutorial.

If you see such need let us know.

(BTW: Python comes with the powerful module “random” - bge.logic.getRandomFloat() is more than redundant)

Great resource thaks a lot monster!

Awesome! Thanks! :smiley:

How to use this to add random value to the property instead of assigning it?

This is not exactly using the logic brick, but it’s exactly what you need. Instead of assigning ( = ), you can use += to add the generated value. -= to subtract. I’d probably run it as a function so you don’t even have to import logic.

You can always use the modified property as input in another property actuator:

-> random actuator [ Property: random ]
-> property actuator [Mode: add Property: value Value: “random”]
alt:
-> property actuator [Mode: assign Property: value Value: “value + random”]