Sometimes we want our robots to have some kind of random behavior. In such cases we need to generate some random values. Now we will take a look at how we can achieve that by using the EV3 programming language.
- 26 Sep 2018
The Random block is used to generate random values. It is the last block in the data palette and its icon is a dice. Take it and put it on the canvas:
From the drop-down menu we can see that the block has two modes - one for generating random numbers and one for generating random logic values i.e. True or False.
First, let us take a look at the numeric mode. In that mode the block has two input parameters. The first one is for the lower bound
and the second one is for the upper bound.
The block has also one output parameter. It returns a random number between the lower bound and the upper bound with each number in between with equal probability.
If you take a random block that generates a random number between 1 and 5 and you then pass the result from it to a switch block, which has the following cases:
the result will be as follows:
- if the random block generates 1, the robot will display "one";
- if the random block generates 2, the robot will display "two";
- if the random block generates 3, the robot will display "three";
- if the random block generates 4 or 5, the robot will display "greater than three".
Next, we have the logic mode. As mentioned above, we can randomly generate true or false using the logic mode. In that mode the block has only one parameter.
It is the probability of returning true in percents. The default value is 50 which means that true and false have equal probability. The output parameter returns true or false.
Courses and lessons with this Tutorial
This Tutorial is used in the following courses and lessons