0% found this document useful (0 votes)
35 views

Advanced Ev3 Programming Lesson: Random Block

This document discusses using the random block in EV3 programming. It describes what the random block does, challenges the reader to test the randomness of the block, provides a solution to fix issues with randomness, and presents an optional bonus challenge to create a Simon game using the random block and sensors.

Uploaded by

Rafa S.C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Advanced Ev3 Programming Lesson: Random Block

This document discusses using the random block in EV3 programming. It describes what the random block does, challenges the reader to test the randomness of the block, provides a solution to fix issues with randomness, and presents an optional bonus challenge to create a Simon game using the random block and sensors.

Uploaded by

Rafa S.C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

ADVANCED

EV3
PROGRAMMING LESSON

Random Block
By Sanjay and Arvind Seshan
Lesson Objectives

ì Understand what the random block does

ì Fix the random block’s “lack of randomness”

ì Create a game using the random block

ì Prerequisites: Math Blocks, Data Wires, Variables, Constants,


My Blocks with Inputs and Outputs

© 2016 EV3Lessons.com, Last edit 6/30/2016 2


What Does the Random Block Do?

ì Random Block (Numeric Mode)


ì Two inputs: The minimum and
maximum values for the output
ì It outputs a number between the
range specified
ì The output is only integers (no
decimals/fractions)

ì Random Block (Logic Mode)


ì One input: The probability of True
being the output
ì It outputs either True of False

© 2016 EV3Lessons.com, Last edit 6/30/2016 3


What Can You Use a Random Block For?

ì As a replacement for dice

ì Making your robot unpredictable (e.g. random animal


movements)

ì Making a Game

© 2016 EV3Lessons.com, Last edit 6/30/2016 4


Challenge 1: Is the Random Block Random?

ì Make a random number generation system to pick a number


between 1 and 3

ì In a loop, record how many times you get each number using
three variables

ì Run your system 1000 times

ì Display the results to the screen

ì What do you notice about the number of times you got #1 vs.
#2 vs. #3?

© 2016 EV3Lessons.com, Last edit 6/30/2016 5


Challenge 1 Solution

Loop 1000 times

Add 1 to the variable decided by the random


block and display to the screen

Get random
Three variables – one Wait before
number
for each number exiting

© 2016 EV3Lessons.com, Last edit 6/30/2016 6


Challenge 1 Discussion

ì You will notice that you got the #1 and #3 about 250 times each.
But you got the #2 around 500 times

ì This is due to a bug in the EV3 that causes the boundary values
(1 and 3 in our example) to occur half as often as the middle
values (2 in our example). This is true no matter what your
range of numbers is.

ì Can you think of how you might fix this problem to get a better
random number?

© 2016 EV3Lessons.com, Last edit 6/30/2016 7


Challenge 2: Fix the “Lack of Randomness”

ì Create a new system that is more random in picking a number


in Challenge 1 and fixes for the bug in the EV3 code

ì Record how many times you get each number and compare the
results to the previous results

© 2016 EV3Lessons.com, Last edit 6/30/2016 8


Challenge 2 Solution
In our solution, we subtract one from the lowest range value and add one to the highest
range value, and reject those two values (so that we eliminate using the boundary values)

Add and Subtract 1 from the range Get random value If random is a boundary value -> repeat

Same code as Challenge 1 except the random block is


replaced with our new My Block

© 2016 EV3Lessons.com, Last edit 6/30/2016 9


Challenge 2 Discussion

ì #1, #2 and #3 have an equal chance of being picked at random


with our fix

© 2016 EV3Lessons.com, Last edit 6/30/2016 10


Bonus Challenge: Create a Simon Game

ì Make a game that is similar to the


Simon Game using your EV3
ì Unfamiliar with the game? See:
Wikipedia Simon Game

ì You can use touch sensors, color


sensors, brick buttons (see Simon
Game by Damien Kee), or even the
Mindsensors PSP-Nx Controller (see
the PSP-Nx Controller Lesson in
Beyond on EV3Lessons.com)
ì Our version (see photo on the right)
uses four color sensors. The code to
play the game can be downloaded
on ev3lessons.com

© 2016 EV3Lessons.com, Last edit 6/30/2016 11


Credits

ì This tutorial was created by Sanjay Seshan and Arvind Seshan


ì More lessons at www.ev3lessons.com

This work is licensed under a Creative Commons Attribution-


NonCommercial-ShareAlike 4.0 International License.
© 2016 EV3Lessons.com, Last edit 6/30/2016 12

You might also like