Scratch – Math Concepts: Online Course
Random Numbers |
- A card game that uses random numbers to form a shuffled deck of cards.
- A dice game uses random numbers to simulate a throw.
- Many activities in the real world (such as traffic on a highway system) appear to be random.
- A single number is not random. Only an infinite sequence can be described as random.
- A program that models them must use random numbers.
What are random numbers?.
- 1, 3, 5, 7, 9, 11, …?
- 3, 7, 0, 7, 7, 4, 1, 5, 6, 1, 7, 8, 5 …?
- 44, 22, 16, 33, 67, 29, 68, 38, 49, 89, …?
- 4, 4, 4, 4, 4, 4, 4, 4, …?.
The pick random () to () block is an operators block and a reporter block. The block will randomly pick a number between and including the numbers used. The default settings will show a random number between 1-10. Scratch provides the pick random block specifically for this purpose.
To drag the first block, place it inside the second block. As you know this random block is a reporter block and cannot be used by itself. For this you need another block, It has to be placed inside this block.
If both numbers are integers, the output returned will also be an integer. If one or both of the number is a float, then output will be in the form of float. Some example with possible outcomes.
Some common uses for the pick random () to () block are:
Example 1: Scratch Program to Random Number (simple Example) |
Scratch Program to Random Number: To do this, we need to follow these steps
- From operators block, drag a random() block on sprite area.
- Continually click on the block and it will report a random string of numbers, 1 though 10.
- Output:
- Change the 10 to 6, Continually click on the block and it will report a random string of numbers, 10 though 6.
- Output:
Example 2: Scratch Program to Random Number using Say() block |
Scratch Program to Random Number using Say() block: To do this, we need to follow these steps
- From operators block, drag a pick random() block on sprite area.
- From looks block, drag a say() block stays for the specified amount of time on sprite area.
- Pick the block “pick random from 1 to 10” and let it replace the value hello! in the say() block.
Output:
Example 3: Scratch Program to two Random Number using Set() and Variable block |
Scratch Program to Two Random Number using using Set() and Variable block: To do this, we need to follow these steps
- Let’s create variables num1 and num2 using variables block.
- Let’s set variables num1 and num2.
- From Events, Drag green flag() block on sprite area.
- From Variables, Drag the set() block ‘set variable to 0’ on sprite area.
- From Operators, Drag the random() block “pick random from 1 to 10” and let it replace the value 0 in the set() block.
- set Num1 variable in a set() block.
- Repeat above three steps for Num2 variable.
- If you want, The cat is going to combine both values and display it at once.
- Then use join() blocks from operators.
- We are going to join the string “Var1:” with its value. After that, we will have to join “Var2:” with its value as well.
- If you want show the values of both variables and then display them as ‘Num1 = 2 and Num2 =10‘. then required a join() block four time.
- From operators, Drag Join() blocks four time in a sprite area.
- From looks block, drag a say() block stays for the specified amount of time on sprite area.
- Pick all four join() block then arrange one within one and let it replace the value ‘hello!’ in the say() block.
- Replace text value from variable ‘Num1 & Num2‘. for example text ‘apple‘ replace from text ‘Num1=‘ and Replace text ‘banana’ from variable ‘Num1‘.
- It is same for Num2 variable.
- When the green flag is clicked, the script activates.
- To run the program. You should get the following result.
Input:
Output: