0% found this document useful (0 votes)
4 views10 pages

CS1102 Lab03B

Lab 03B focuses on understanding discrete and continuous random numbers through various programming exercises. It encourages self-discovery and problem-solving in coding, particularly in generating random numbers and manipulating them to achieve desired outcomes. The lab also introduces concepts such as overflow in binary addition using two's complement representation.

Uploaded by

issac156342
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)
4 views10 pages

CS1102 Lab03B

Lab 03B focuses on understanding discrete and continuous random numbers through various programming exercises. It encourages self-discovery and problem-solving in coding, particularly in generating random numbers and manipulating them to achieve desired outcomes. The lab also introduces concepts such as overflow in binary addition using two's complement representation.

Uploaded by

issac156342
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/ 10

Lab03B - Page 1 of 10 Howard Leung / CS1102 Introduction to Computer Studies / 2024-2025 Semester A

Lab 03B More on Random Numbers and Binary Numbers

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.

Task 1.1 More on Discrete Random Numbers


As introduced in Lab 02, a discrete random number is a random number that takes countable distinct values
as possible outcomes. For example, a coin toss will generate a random outcome of either head or tail. If we
use 0 to represent head and 1 to represent tail, as well as denote c as a random number representing the
outcome of a coin toss, then c∈{0,1}, where {} denotes a set and in this case this set contains 2 elements, 0
and 1. The symbol ∈ means “an element of” so c∈{0,1} means that c is an element of the set {0,1}. In other
words, the value of c is either 0 or 1. We have also learnt from Lab 02 that you can write the following code
to generate such a random number in Scratch:

Exercise 1.1A: Consider the scenario in which you are trying to choose between set lunch 1 and set lunch 2.
You do not know which one to pick so you write a program to help you decide by generating a discrete
random number s which has possible outcomes of 1 or 2, i.e., s∈{1,2}. How can you manipulate c so that you
can obtain s that satisfies the requirement without generating a new random number? In other words, start
with the above code that assigns a random number c with possible outcomes 0 or 1, then assign s in terms of
c without using another pick random block. (Hint: try to derive the relationship between c and s, e.g., if you
add 1 to c, then what are the possible values?). The following program illustrates how to obtain s from c
such that s is a discrete random number where s∈{1,2}:

Exercise 1.1B: Consider the scenario in which your investment may have 2 outcomes: double or nothing. This
means that for the amount of x dollars that you have invested, you may receive 2x dollars (double) or 0x=0
dollar (nothing). Now try to generate a discrete random number m to model this multiplicative factor of
either 0 or 2, i.e., f∈{0,2}. Similar to Exercise 1.1A, start with the code that assigns a random number c with
possible outcomes 0 or 1, then assign f in terms of c without using another pick random block. (Hint: can you
multiply c with some constant value to solve this problem? What would be the constant value to multiple
with c)?
Lab03B - Page 2 of 10 Howard Leung / CS1102 Introduction to Computer Studies / 2024-2025 Semester A

The following program illustrates how to obtain f from c such that f is a discrete random number where
f∈{0,2}:

Exercise 1.1C: Consider the scenario in which you want to choose between eating 1 meal a day or 3 meals a
day. You do not know which one to pick so you write a program to help you decide by generating a discrete
random number m which has possible outcomes of 1 or 3, i.e., m∈{1,3}. How can you manipulate c so that
you can obtain m that satisfies the requirement without generating a new random number? In other words,
the code that assigns a random number c with possible outcomes 0 or 1, then assign m in terms of c without
using another pick random block. (Hint 1: try to derive the relationship between c and m. Hint 2: Refer to the
answer for Exercises 1.1A and 1.1B and see if you can combine their result).

Exercise 1.1D: Consider the scenario in which you want to change the temperature setting by either
increasing it or decreasing it by one degree. You want to write a program to help you decide by generating a
discrete random number t which has possible outcomes of -1 or 1, i.e., t∈{-1,1}. How can you manipulate c
so that you can obtain t that satisfies the requirement without generating a new random number? In other
words, the code that assigns a random number c with possible outcomes 0 or 1, then assign t in terms of c
without using another pick random block. (Hint 1: try to derive the relationship between c and t. Hint 2:
Refer to the answer for Exercises 1.1C to get some inspiration).

Task 1.2 Combining Discrete Random Numbers


In this task we will illustrate some examples on combining discrete random numbers and determining the
possible outcomes with their corresponding probabilities.

Exercise 1.2A: Given the following code with 2 discrete random variables c1 and c2, what are the possible
values of their sum k?

You can list the possible combinations of the input c1 and c2, and work out the sum k in each case as shown
in the following table:
c1 c2 k = c1+c2
0 0 0
0 1 1
1 0 1
1 1 2

From the above table, you can see that the possible values of k are 0, 1, 2.

Exercise 1.2B: The variable k obtained in the code from Exercise 1.2A has 3 possible values. Is it suitable to
use it to generate a random Rock, Paper, Scissors move so that each move has equal probability of
occurrence? For instance, does it satisfy the requirement if we use 0 to represent Rock, 1 to represent Paper,
2 to represent Scissors?
Lab03B - Page 3 of 10 Howard Leung / CS1102 Introduction to Computer Studies / 2024-2025 Semester A

From the above table, it can be seen that out of 4 cases of the input combinations, k is equal to 0 one time,
equal to 1 two times, equal to 2 one time. This means that the probabilities of occurrence for k to be equal
to 0,1,2 are 1/4, 1/2(=2/4), 1/4 respectively. In other words, the outcomes do not have equal probabilities of
occurrence so it does not satisfy the requirement.
Does it mean that you cannot use the binary random variables c1 and c2 to generate a random Rock, Paper,
Scissors move (3 possible outcomes) with equal probability of occurrence?
You still can. For instance, consider the input combinations and the corresponding outcomes in the following
table:
c1 c2 Outcome
0 0 Rock
0 1 Paper
1 0 Scissors
1 1 Discard this result and generate random values of c1 and c2 again
Repeat this process until c1 and c2 fall into 1 of the above 3 cases

Rock, Paper, Scissors will have equal probabilities of occurrence since each of them only appear on exactly 1
out of 4 input combinations. And the last case just means repeating the whole process again so it will not
affect the probabilities among Rock, Paper, Scissors. Although in the worst case scenario the last case may
occur again and again and it will take many iterations to get the outcome, this worst case scenario with too
many iterations is not likely to happen if c1 and c2 both are both uniformly distributed among outcomes 0
and 1 (so c1 and c2 should not be equal to 1 indefinitely in all iterations). Alternatively for this particular
problem it is simpler to use the following block if we are not limited to binary random variables:

Exercise 1.2C: Consider the following code and try to determine the possible values of each variable after
each block:

(a) What are the possible values of c1? Write it with the set notation using ∈, { }.

(b) What are the possible values of t? Write it with the set notation using ∈, { }.

(c) What are the possible values of c2? Write it with the set notation using ∈, { }.

(d) What are the possible values of n? Write it with the set notation using ∈, { }.

(e) What are the possible values of p? Write it with the set notation using ∈, { }. (Hint: Consider all input
combinations of t and n and determine the output p in each case)
Lab03B - Page 4 of 10 Howard Leung / CS1102 Introduction to Computer Studies / 2024-2025 Semester A

Task 1.3 More on Continuous Random Numbers


A continuous random number introduced in Lab 02 is a random number with possible outcomes that are not
countable. For example, pouring a random amount of water from a bottle with 100mL of water. You may get
0mL (nothing), 100mL (everything), 12.3456789mL, any floating-point number between 0 to 100, etc. Given
a possible outcome, e.g., 92.6mL, it is not possible to determine the “next” number that is larger than it with
no other numbers in between, i.e., for whatever number you pick as the “next” number, there always exist
numbers in between them. For instance, if you claim that 92.7mL is the number next to 92.6mL, I can show
you that 92.65mL is between 92.6mL and 92.7mL to counter your claim. Now can you tell the difference
between discrete and continuous random numbers? Is it more suitable to model the length of a rope as
discrete or continuous random number?

Very often a continuous random number is generated as a floating-point number in the range of 0 to 1. Let
us denote r as such continuous random number and the following Scratch code can be used to generate it:

The above code will generate a random number r as a floating-point number between 0 and 1, i.e., 0 ≤ r ≤ 1.
Note that both 0 and 1 are inclusive, meaning that it is possible to get a value of 0 or 1. We can express it as
r∈[0,1], where [0,1] denotes the continuous number range from 0 to 1. Note the direction of the square
brackets which indicate that both end points are included. If an end point is not to be included, e.g.,
0 ≤ q < 1 can be expressed as q∈[0,1[ where 0 is included but 1 is not included.

You can imagine the above block as a number line from 0 to 1, with the random number as a point randomly
picked from this line:

0 r 1
Now we would like to manipulate this random number to generate random numbers in other ranges.

Exercise 1.3A: How can you manipulate r∈[0,1] so that you can obtain another random number v such that
v∈[3,4]? In other words, start with the above code that assigns a continuous random number r, then assign v
in terms of r without using another pick random block?
You may wonder why we do not directly change the parameters in the pick random block from 0,1 to 3,4 but
rather study the manipulation of r to get v? Although in Scratch it can be done easily, in other programming
languages it may only provide you with a continuous random number generator that gives you a floating-
point number between 0 and 1, and you need to manipulate this continuous random number in order to
obtain the output number in the desired range of possible values.
Think of this problem graphically as below:

0 r 1 3 v 4
You can see that v can be obtained from r by moving it to the right by 3 units, i.e., v = r + 3.
The following program illustrates the corresponding code in Scratch:
Lab03B - Page 5 of 10 Howard Leung / CS1102 Introduction to Computer Studies / 2024-2025 Semester A

Exercise 1.3B: How can you manipulate r∈[0,1] so that you can obtain another random number u such that
u∈[0,2]? In other words, start with the above code that assigns a continuous random number r, then assign
u in terms of r without using another pick random block?
Think of this problem graphically as below:

0 r 1 0 u 2
You can see that u can be obtained from r by stretching it so that its length is doubled, i.e., u = 2r.
The following program illustrates the corresponding code in Scratch:

Exercise 1.3C: How can you manipulate r∈[0,1] so that you can obtain another random number w such that
w∈[3,5]? In other words, start with the above code that assigns a continuous random number r, then assign
w in terms of r without using another pick random block? (Hint 1: how do you need to stretch/compress r so
that it has the same length as w? Hint 2: how much do you need to move the result to the left or to the right,
i.e., how much should you further add/subtract after the scaling from Hint 1?)
The following program illustrates the corresponding code in Scratch:

Now you should be able to manipulate r to give you any range (any starting point with any length of the
resulting range, e.g., [0.5,1], [-3,5], etc.).

Task 1.4 Discrete vs Continuous Random Numbers


Recall that a discrete random variable gives a random value from a set with distinct elements that are
countable while a continuous random variable gives a random value from a set with possible values that are
not countable. In previous examples, we illustrated discrete random variables with integers and continuous
random variables with floating-point numbers. Note that just because a set contains floating-point numbers
does not necessarily mean that it is continuous.

Exercise 1.4A: Consider the following code and try to determine the possible values of each variable after
each block and whether each variable is discrete or continuous random variable:
Lab03B - Page 6 of 10 Howard Leung / CS1102 Introduction to Computer Studies / 2024-2025 Semester A

(a) What are the possible values of g? Write it with the set notation using ∈, { }, [], etc. Is it a discrete
random variable or a continuous random variable?

(b) What are the possible values of h? Write it with the set notation using ∈, { }, [], etc. Is it a discrete
random variable or a continuous random variable?

(c) What are the possible values of d? Write it with the set notation using ∈, { }, [], etc. Is it a discrete
random variable or a continuous random variable?

(d) What are the possible values of e? Write it with the set notation using ∈, { }, [], etc. Is it a discrete
random variable or a continuous random variable?

Task 1.5 Checking Overflow with 2-Bit Two’s Complement Representation


In this task, we would like to work with 2-bit binary numbers with two’s complement representation. In
particular, we would like to check if overflow occurs when two 2-bit binary numbers are added.
Denote the two 2-bit binary numbers as a1 a0 and b1 b0. Denote s1 s0 as a 2-bit binary number representing
the sum. We will use the variable y to indicate if overflow occurs from the addition, with y=0 to denote no
overflow and y=1 to denote overflow. Now we would like to derive an equation for y given the input bits a1,
a0, b1, b0. So you need to consider all the input combinations of a1, a0, b1, b0, and in each case calculate
the sum s1 s0 and determine the value of y by checking whether overflow occurs. It will be a very good idea
for you to list the input combinations (a1, a0, b1, b0) and work out the required output values (s1, s0, y) in a
table. Refer to Lecture 02 slides 31-32 about how to efficiently enumerate all the binary input combinations.
Complete the following table:
a1 a0 b1 b0 s1 s0 y

(a) How many cases out of 16 do overflow occur?

(b) What are the corresponding decimal values of the input binary numbers a1 a0 and b1 b0 in each
case when overflow occurs?

(c) Can you observe a pattern among a1, b1 and s1 when overflow occurs? To make sure that you get
the correct pattern, verify that such pattern does not exist in case there is no overflow.
Lab03B - Page 7 of 10 Howard Leung / CS1102 Introduction to Computer Studies / 2024-2025 Semester A

Complete the following tasks after finishing Task 1 in class or after class.

Task 2.1
In Task 1.2, a code was given to generate a sum of 2 discrete random variables c1 and c2 where each one has
a possible outcome of 0 or 1, i.e.,

In this problem, you will explore the sum of N discrete random variables c1, c2, c3, …, cN where each one
has a possible outcome of 0 or 1.

1) For N = 3, denote k3 = c1+c2+c3. Complete the following table by considering the possible
combinations of the input c1, c2, c3 and determine the corresponding output k3.
c1 c2 c3 k3

What are the possible values of k3? How many input rows from the above table correspond to each
value of k3?

2) For N = 4, denote k4 = c1+c2+c3+c4. What are the possible values of k4? How many input
combinations of c1,c2,c3,c4 correspond to each possible value of k4?

3) For a general value of N, what are the possible values of kN = c1+c2+c3+…+cN? How many input
combinations of c1,c2,c3,…,cN correspond to each possible value of kN? (Search online for
information related to “Binomial coefficients” and “Pascal’s triangle”).

4) For a very large value of N, if you count the number of input combinations of c1,c2,c3,…,cN that
sum up to each possible value of kN and look at the resulting distribution, what kind of distribution
would it look like? (Search online for information related to “The Central Limit Theorem for Sums”).

Task 2.2
Consider the following code and try to determine the possible values of each variable:
Lab03B - Page 8 of 10 Howard Leung / CS1102 Introduction to Computer Studies / 2024-2025 Semester A

1) What are the possible values of r? Write it with the set notation using ∈, { }, [], etc. Is it a discrete
random variable or a continuous random variable?

2) What are the possible values of x? Write it with the set notation using ∈, { }, [], etc. Is it a discrete
random variable or a continuous random variable?

3) What are the possible values of y? Write it with the set notation using ∈, { }, [], etc. Is it a discrete
random variable or a continuous random variable?

4) What are the possible values of b? Write it with the set notation using ∈, { }, [], etc. Is it a discrete
random variable or a continuous random variable?

5) If the value of b is 0, which other variable is z equal to?

6) If the value of b is 1, which other variable is z equal to?

7) What are the possible values of z? Write it with the set notation using ∈, { }, [], etc. Is it a discrete
random variable or a continuous random variable?

Task 2.3
Consider the following truth table with 3 input a, b, s and the output y. See if you can derive a formula to
express y in terms of a, b, s before looking at the hints below the table.
Truth Table 1
a b s y
0 0 0 0
0 0 1 1
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 0
1 1 0 1
1 1 1 0
Lab03B - Page 9 of 10 Howard Leung / CS1102 Introduction to Computer Studies / 2024-2025 Semester A

Hints:

1) If you focus on the 2 cases in which the output y is 1, you can see that they occur with the input (a,b)
= (0,0) or (1,1). So if you consider the following truth table:
Truth Table 2
a b y1
0 0 1
0 1 0
1 0 0
1 1 1

Can you derive a formula relating the output y1 with the input a, b?

If you apply this formula derived for y1 alone to the Truth Table 1 above, will it work for y?

2) If you focus on the 2 cases in which the output y is 1, you can see that they occur with the input (b,s)
= (0,1) or (1,0). So if you consider the following truth table:
Truth Table 3
b s y2
0 0 0
0 1 1
1 0 1
1 1 0

Can you derive a formula relating the output y2 with the input b, s?

If you apply this formula derived for y2 alone to the Truth Table 1 above, will it work for y?

3) Can you combine the formula you derived for y1 with the formula you derived for y2, are you able to
derive a formula that can for y in the Truth Table 1?

Task 3 Complete the Post-Lab Questions


You should complete the “Post-Lab 03B Questions” on the Canvas course page.

Task 4 Further Discussions


If you have any questions related to this lab, you should first ask the lab tutor or the TA during your lab
session immediately. If you still have questions after the lab session, then you can first check whether your
question has already been posted by another student on the Canvas course page, under this Discussion page.
If not, then you can post a new question there.

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
Lab03B - Page 10 of 10 Howard Leung / CS1102 Introduction to Computer Studies / 2024-2025 Semester A

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.

You might also like