Python random Module
PCAP objective 1.3 is generate random values using the random module. The functions are short and the two things examined are their bounds - randint includes both ends and randrange does not - and the seed, which makes the sequence reproducible. Every example on this page is seeded, because an unseeded one could not be shown with its output at all.
Modules and Packages Guide 6 of 25 Intermediate
- OSUbuntu 26.04 LTS
- Python3.14.4
- pip25.1.1
- TimeAbout 14 min
random.sample no longer accepts a set as its population (removed in Python 3.11); pass a list or a range. Everything else the syllabus names is unchanged.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Python 3.14.4 - the only machine this path needs | 2 Core | 4 GB | 50 GB |
This guide includes
Use this for the random objective. This matters because randint includes both ends and randrange does not - and with the seed fixed the numbers are entirely predictable, which is how the exam asks about them.
- using the seven functions the objective names, with the seed fixed
- seeing the same seed give the same numbers, and no seed give different ones
- separating
randintfromrandrangeat the upper end - finding that
shufflechanges the list andsampledoes not - meeting the three ways random refuses
Before you start
-
The functions, with the seed fixed
-
The same seed gives the same numbers
-
With no seed, the numbers differ between runs
-
randint includes both ends, randrange does not
-
shuffle changes the list, sample does not
-
How random refuses
-
What the exam does with this objective