Lego EV3 Buttons Programming Random Block - Title

Lego MINDSTORMS EV3: Random Block In Detail

In Lego Mindstorms by Glenn TurnbullLeave a Comment

In this post we will cover the Random programming block used within the Lego MINDSTORMS EV3 programming software. We will cover all the available options and explain them by using them using step by step example programs.

What is the Random Block?

The Random block has 2 modes and will either output a random numeric or logic (true or false) value. It’s available from the red data operations palette:

Lego EV3 Buttons Programming Random Block

What are Random Block Modes?

The two modes that the Random block offer are:

Numeric Mode

The numeric mode will generate a random integer (number) between the lower and upper input numbers. Each number within the range is equally likely to be chosen. One thing to be mindful of is the random number does include the upper and lower bound values.

Lego EV3 Buttons Programming Random Block - Numeric Mode

The lower bound (-) is the 1 and the upper (+) is 10 in the image above. The output value (dice) is designed to be wired into another block. We go over how to use the block next.

Logic Mode

The logic mode will output either a true (1) or false (0) value. It offers one input named “probability of true”. This is a percentage value (0-100) which can be used to control the probability that true is chosen. 50 indicates that there is a 50% chance that true (or false) could be selected.

Lego EV3 Buttons Programming Random Block - Logic Mode

How to Use the Random Block

The random block is designed to have its output value wired into another block. This destination block could be anything from a variable (write mode), a move tank or a switch block.

In the example below we show how a random block could be wired into a EV3 Display block. The random block generates a number between 1 and 5 and outputs to the screen for 2 seconds:

Lego EV3 Buttons Programming Random Block - Random Block Example 1

Can the EV3 Random Block Handle Decimals?

Unfortunately the Random block cannot handle decimal numbers and only works with whole numbers.

EV3 Random Block Example Program 1: Random Movement

In the first example we’ll create a program that will randomly move your Lego MINDSTORMS EV3 robot for 10 seconds. This program is a little random (which is the point right?) so make sure your robot is in a safe spot so it does not drive off the edge of a table.

Program Logic:

  1. Generate a random number between -100 and 100 to control the direction of the robot
  2. Use the random number to steer the direction of the robot
  3. Loop program for 10 seconds

For this build I recommend the Explor3r It’s a quick and easy to assemble and can be built with either the Education and Home Lego Lego MINDSTORMS kit. The build instructions can be found here.

Let’s start the program:

  1. Loop the program for 10 seconds:
    • Drag and drop an orange Loop block next to the start button
      • Set its mode to Time Indicator
      • Set the Seconds input to 10
Lego EV3 Buttons Programming Random Block - Random Block Example Program 1 - Step 1
  1. Generate a random number between -100 and 100 and use this number to turn the robot.
    • Drag and drop a red Random block inside the loop:
      • Update the lower bound value to -100
      • Update the upper bound value to 100
    • Drag and drop a green Move Steering block next to the random block inside the loop
      • Wire the Value output from the Random block into the Steering input of the Move Steering block
      • Set the number of rotations input to 2
Lego EV3 Buttons Programming Random Block - Random Block Example Program 1 - Step 2

That’s the last step of the program – hit the download and run button to test out the program.

Tip: Try adjusting the random number range to make the direction changes more subtle.

EV3 Random Block Example Program 2: Dancing Robot

This this last example program we will use the Random block’s logic mode to make the EV3 robot dance. As with the example program above we recommend using the Explor3r build. Instructions van be found here.

Program Logic:

  1. Generate a true or false value
  2. Use output (true or false) to determine which way to turn the robot ( right or left)
  3. Loop the program for 10 seconds

Let’s get started:

  1. Loop the program for 10 seconds:
    • Drag and drop an orange Loop block next to the start button
      • Set its mode to Time Indicator
      • Set the Seconds input to 10
Lego EV3 Buttons Programming Random Block - Random Block Example Program 2 - Step 1
  1. Generate a random true or false and use this value to determine the robot’s direction:
    • Drag and drop a red Random block into the loop
      • Set its mode to Logic
      • We’ll leave the probability of true at 50%
    • Drag and drop an orange Switch block next to the Random block
      • Set its mode to Logic
      • Wire the Random block’s Value output into the Switch block’s Logic input
    • Turn the robot right if the value is True:
      • Drag and drop a green Move Steering block into the True case of the Switch block
        • Set its mode to On for Rotations
        • Set Rotations to 2
        • Set the steering input to 50 (right)
    • Turn the robot left if the value is False:
      • Drag and drop a green Move Steering block into the False case of the Switch block
        • Set its mode to On for Rotations
        • Set Rotations to 2
        • Set the steering input to -50 (left)
Lego EV3 Buttons Programming Random Block - Random Block Example Program 2 - Step 2

Press the download and play button to test your dancing robot program.

Tip: this robot dance could be improved by using a random number between 1 and 4 and use the output numbers to:

1. Move right

2. Move Left

3. Go forwards

4.Go backwards

EV3 Random Block Example Program 3: Guessing Game

We detailed a guessing game program using the Random block in our EV3 Brick button post. Check out the Example program 1 mid-way down the page from here.

We hope you enjoyed learning about the Lego MINDSTORMS EV3 Random block as much as we did writing about it. Feel free to leave a comment if you have any questions or suggestions for future posts.

Leave a Comment