JPJC python random module notes
Uploaded by HiThere123 · 10 March 2024
Preview
Text from the first pages2024 JC 1 H2 Computing (9569) 1 Random Number Random numbers are useful for a variety of purposes, such as generating data encryption keys, simulating, and modelling complex phenomena and for selecting random samples from larger data sets. They have also been used aesthetically, for example in literature and music, and are of course ever popular for games and gambling. When one single random number is concern, that random number should be selected from a set of possible values , where the selection of every single value in the set is equally probable, i.e., a uniform distribution. However, when considering a sequence of random numbers, the selection of each random value must be statistically independent of the others. Random Number Generator (RNG) With the advent of computers, programmers recognised the need for a means of introducing randomness into a computer program. However, surprising as it may seem, it is difficult to get a computer to do something by chance. Random numbers take on a large role in the world of computing. In real life, a fundamental use of random numbers can be seen in lotteries and lucky draws , where computer programs are used to generate random numbers to `decide the winning tickets. In the world of computing, random numbers can be used to generate data encryption keys, or to select an independent random sample from the data set. A computer follows its instructions blindly and is therefore completely predictable. (A computer that does not follow its instructions in this manner is considered broken.) There are two main approaches to generating random numbers using a computer: 1. Pseudo-Random Number Generators (PRNGs) , 2. True Random Number Generators (TRNGs) . The approaches have distinct characteristics, and each has its pros and cons.
2024 JC 1 H2 Computing (9569) 2 True Random Numbers Random numbers obtained from certain uncontrollable physical phenomena. The physical phenomenon can be very simple, like the little variations in somebody's mouse movements or in the amount of time between keystrokes. A good physical phenomenon to use is a radioactive source. The points in time at which a radioactive source decays are completely unpredictable, and they can quite easily be detected and fed into a computer , avoiding any buffering mechanisms in the operating system. For most real-life applications, a pseudo-random number is sufficient. For example, a CD player in “random” mode is really playing in pseudo-random mode, with a pattern that is discernible if you listen carefully enough. Using pseudo-random numbers is perfectly acceptable in this case because there’s no quantitative advantage in the degree of randomness. Can a computer generate a truly random number? Computers are deterministic devices (i.e. given and using the same input, it will follow a fixed set of instructions and therefore produce the same output). For true random numbers to be generated, the device generating the random number has to produce totally unpredictable and unbiased output from the same input source . It is impossible to apply reverse engineering on truly random numbers . There are devices that generate numbers that claim to be truly random. They rely on unpredictable processes like thermal or atmospheric noise rather than human-defined patterns. You can program a machine to generate what can be called “random” numbers, but the machine is always at the mercy of its programming . On a completely deterministic machine you cannot generate anything you could really call a random sequence of numbers, because the machine is following the same algorithm to generate them . Typically, that means it starts with a common ‘seed’ number and then follows a pattern. The results may be sufficiently complex to make the pattern difficult to identify, but because it is
2024 JC 1 H2 Computing (9569) 3 ruled by a carefully defined and consistently repeated algorithm, the numbers it produces are not truly random. They are what we call ‘pseudo-random’ numbers. Pseudo-Random Number Generators (PRNGs) Pseudo Random Number Generator (PRNG) refers to an algorithm that uses mathematical formulas to produce sequences of random numbers. PRNGs generate a sequence of numbers approximating the properties of random numbers. A PRNG starts from an arbitrary starting state using a seed state. With a starting point, a sequence of pseudo-random numbers can be generated in a short time. Additionally, these numbers can also be reproduced later. Hence, the random numbers generated by PRNGs are deterministic and efficient. How a fundamental PRNG work? The Linear Congruential Generator is most common and oldest algorithm for generating pseudo-randomised numbers. The generator is defined by the recurrence relation: By choosing the following values: m = 9, a = 2, c = 0, X0 = 1 We have, Xn+1 = (2Xn + 0) % 9 Hence, X0 = 1 X4 = (16 % 9) = 7 X1 = (2 % 9) = 2 X5 = (14 % 9) = 5 X2 = (4 % 9) = 4 X6 = (10 % 9) = 1 X3 = (8 % 9) = 8 X7 = (2 % 9) = 2 Given that: X is a sequence of pseudo – random values, m > 0, (m – modulus), 0 < a < m (a - multiplier), 0 ≤ c < m (c - increment), 0 ≤ X0 < m (X0, seed or start value) Xn+1 = (aXn + c) % m
2024 JC 1 H2 Computing (9569) 4 Characteristics of a PRNG Efficient: PRNG can produce many numbers in a short time and is advantageous for applications that need many numbers. Deterministic: A given sequence of numbers can be reproduced later if the starting point in the sequence is known. Determinism is handy if you need to replay the same sequence of numbers again at a later stage. Periodic: PRNGs are periodic, which means that the sequence will eventually repeat itself. While periodicity is hardly ever a desirable characteristic, modern PRNGs have a period that is so long that it can be ignored for most practical purposes. Important You are required to know how to generate random numbers in their chosen programming language. You are likely required to use random numbers in the problems they solve and so, they should also be able to include them in their pseudocode. Using a random number generator in Pseudocode It is important to note that any use of a function to generate random numbers must be clearly explained. The following are two commonly used random number generator functions: 1. RND(): generates and returns a single random real number between 0 and 1. 2. RANDOMBETWEEN(min, max) : generates a random integer between the integers min and max. RND() function Returns a single random number.
2024 JC 1 H2 Computing (9569) 5 The RND() function returns a float value less than 1 but greater than or equal to 0. (i.e. [0.0, 1.0)) The value of number determines how RND()generates a random number: To produce random integers in a given range, the following expression can be used: Pseudocode – To generate a random integer value from 1 to 6 (both incl) FUNCTION RANDOMBETWEEN (min: INTEGER, max: INTEGER) RETURNS INTEGER: DECLARE rand_no: INTEGER rand_no = INT((max - min + 1) * RND() + min) RETURN rand_no ENDFUNCTION // -----------Main program------------- DECLARE random_no: INTEGER random_no = RANDOMBETWEEN(1, 6) Generating a Random Number in Python randint(a, b) Returns a random integer N such that a <= N <= b. i.e. [a,b] random() Returns a random floating point number in the range [0.0 to 1.0). INT((upperbound - lowerbound + 1) * RND() + lowerbound) // upperbound is the highest number in the range, and // lowerbound is the lowest number in the range
2024 JC 1 H2 Computing (9569) 6 randrange(start, stop, step) Return a randomly selected element from randrange(start, stop, step). Expected output: Expected output of randrange(1, 10, 3) //incl 1, not incl 10 Integer values [1, 4, 7] True-Random Number Generators (TRNGs) In comparison with PRNGs, TRNGs extract
Content continues in the PDF. Download PDF
Related notes
- NYJC 2026 Prelim P2Exam Papers · 2026
- NYJC 2026 Prelim P1Exam Papers · 2026
- DHS 2026 Y6 H2 Computing Prelim Paper 2_finalExam Papers · 2026
- ACJC 2026 JC2 Computing Prelim Paper 2 (Practical)Exam Papers · 2026
- 2026_NJC Prelim_Computing_P2.pdfExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P2_finalExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_markschemeExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_finalExam Papers · 2026
- 2026 ACJC Prelim Computing Paper 2Exam Papers · 2026
- 2024 ACJC Computing PromoExam Papers · 2024
- 2023 ACJC Promo QPExam Papers · 2023
- 2022 ACJC Computing Promo Paper 2Exam Papers · 2022
- See all H2 Computing notes

