How to generate random values in EV3 Pro Preview

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.

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #932
  • 26 Sep 2018

Random block

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. 

content picture

Numeric mode

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

content picture

and the second one is for the upper bound.

content picture

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.

Example

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".

Logic mode

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.

content picture

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

Image for Instructors Remote Training
  • 136
  • 280:11
  • 156
Image for Switch
  • 7
  • 0
  • 7
  • 3d_rotation 1
Image for Robotics with LEGO - Level 4.0 - Kinesthetic and Memory Game (in development)
  • 59
  • 2:58
  • 200