CS1102 Lab02
CS1102 Lab02
Not to be redistributed
to Course Hero or any
General Information
other public websites
What you should do
• You should follow the instructions step by step especially if this is your first programming course.
• You should try to come up with the code yourself as much as possible. Do not be afraid of making mistakes,
since debugging (finding out where your code goes wrong and fixing it) is part of the learning process.
• We do not give out model programs to the exercises. There can be multiple ways to write the code that
solves the same problem. It is important that you build up the program logic yourself instead of merely
looking at some code that you do not understand. At any time if you are lost or if you have any questions,
feel free to ask the instructor, tutor, or teaching assistant and we will be very happy to help you.
Self-Discovery
• Most lab tasks are designed to be relatively simple such that you can take the time to think about the
related underlying concepts. Besides, we also encourage you to discover things on your own which may not
be specified in the tasks.
Note that the random numbers generated by computer are often not true random numbers but are called
pseudo-random numbers instead. True random numbers are non-deterministic, meaning that there is no
way for you to determine what number comes next even if you have the entire history of all the numbers
generated in the past. On the other hand, pseudo-random numbers are generated by computer algorithms
such that it is like showing numbers successively from a given list that repeats itself (thus you can determine
the pattern if you wait long enough). Although the list may be very long for you to notice the pattern
practically, it is still possible to determine the numbers that will show up if you know the algorithm thus
pseudo-random numbers are deterministic.
Start the Chrome browser on your notebook and go to the webpage https://scratch.mit.edu/ and sign in
with the username and password of your account that you created in Lab 01.
If you click the above block, then you will see that a random integer in the range of 1 to 10 (including both 1
and 10) is generated each time.
In Scratch, create a new project and put down Lab 2 Task 1.2 as the project name. Upload the sprite file
question-dice.sprite2 from the notebook to this new project by clicking on the button on the
bottom right as shown in the following screenshot (hover the mouse over the blue cat icon to expand the
options and choose the upload icon on top):
For this task we do not need the Scratch Cat so we can hide it. One way to hide a sprite is to click on that
sprite and then click on the “hide” block from the Looks block category:
Now we want the program to display a number ranging from 1 to 6. For this, we create a variable to store
this number. Click on the “Make a Variable” block from the Variables block category and put down dice as
the variable name:
Lab02 - Page 3 of 8 Howard Leung / CS1102 Introduction to Computer Studies / 2024-2025 Semester A
You can change the look of the variable. Now right click on the variable dice and choose large readout.
Alternatively, you can also double click on it to change its look until you get the look you like.
Now we want to add the code to display a random number from 1 to 6 when the dice sprite is clicked.
To do:
1) Click the question-dice sprite to make sure that this sprite is active (instead of the Scratch cat)
5) To create the visual effect of the dice throwing, add the blocks 3 times before the
setting the value of the variable from the previous step.
Now you can try your code by clicking on the dice sprite and see a new random number displayed after the
dice movement.
Lab02 - Page 5 of 8 Howard Leung / CS1102 Introduction to Computer Studies / 2024-2025 Semester A
First we should determine the range of the random number to be generated. The original fry measures 1
inch so the kid may obtain at most 1 inch (the entire fry) if he is lucky. Alternatively, he may obtain 0 inch
(nothing) if he is really unlucky. So the range of the random number is from 0 to 1. Besides, he may just get a
fraction of the fry so he may get something like 0.5 inch (half), 0.25 inch (25%), 0.1234567 inch, etc. As a
result, in the code, you need to generate a random continuous floating-point number (a number with
decimal places) that ranges from 0 to 1. Note that if you represent all floating-point numbers on a number
line, these numbers change continuously and given a certain number, it is not possible for you to tell what is
the next number (as opposed to the case with integers where you can tell what is the next number).
If you change the numbers in the block from “1 to 6” to “0 to 1”, would it be able to
generate the required random floating-point number? If you change these parameters to “0.0 to 1”, would it
be able to generate the required floating-point number? Note that as long as you put down a floating-point
number in any of the 2 input parameters, a random floating-point number will be generated, with the range
bounded by the 2 input numbers (inclusive). For example, while the block gives you
possible value of 1, 2 or 3, any of these blocks , or
would give you a floating-point random number x where 1≤x≤3.
The x coordinate of the stage area ranges from -240 (left) to 240 (right) and the y coordinate ranges from -
180 (bottom) to 180 (top). The center of the stage has the coordinates (0,0). The coordinate system of the
stage area is illustrated below:
Lab02 - Page 6 of 8 Howard Leung / CS1102 Introduction to Computer Studies / 2024-2025 Semester A
y
180
x
-240 240
0
-180
As a result, the above block would be equivalent to the following block for setting the sprite in a random
position when the program starts:
Complete the following tasks after finishing Task 1 in class or after class.
Task 2.1
1) What are the possible values of x as a result of the following block?
2) How do you modify from the above block such that x∈{-1, 1}, i.e., the value of x is either -1 or 1?
3) How can you generate a random integer x∈{0, 1, 2} but instead of equal probabilities, the
probabilities of getting 0,1,2 are 0.25,0.5,0.25 respectively?
5) How do you modify from the above block such that y∈{1.5, 2.0, 2.5, 3.0, 3.5, 4.0}?
Lab02 - Page 7 of 8 Howard Leung / CS1102 Introduction to Computer Studies / 2024-2025 Semester A
7) Does p from the above block have the same set of possible outcomes as q from the following block?
If not, what are the differences?
Task 2.2
1) Generate a random floating-point number x such that -3≤x≤-1 or 1≤x≤3, i.e., x∈[-3,-1] ∪ [1,3]
Hint: refer to Task 2.1 step 2)
2) Generate a random floating-point number x such that 0≤x≤2 or 4≤x≤6, i.e., x∈[0,2] ∪ [4,6]
Hint: refer to 1)
Task 2.3
Write a program so that it satisfies the following specifications when the program starts:
2) A square of size 200 centered at (0,0) is drawn using blocks from the Pen category
3) The Cat moves to a random position with its center inside the drawn square. Note that when the
center of the Cat is inside the square, part of the Cat may still fall outside it.
Lab02 - Page 8 of 8 Howard Leung / CS1102 Introduction to Computer Studies / 2024-2025 Semester A
On the other hand, you should also reflect on what you have learnt in this lab. Then you can try to come up
with problems to challenge your classmates. You can post your problem on the Discussion page with link
shown above. One should be able to solve your problem by using what he/she learns in this lab. You will not
get extra marks by posting a challenging problem or solving a challenging problem posted by another
student, but you will earn your fame so that you can impress the course leader, the lab tutors, and your
classmates.