Advanced Python Development Course
Chapter
>
Level
Math Modules
Random Module
Objective
Run simulations and tests to prepare for any errors and setbacks during the farmland construction by using the random module.
Crossing the annex there is an office that handles some administrative duties handling some paperwork regarding shipping, housing and logistics for the construction. It would be a good idea to run some scenarios using the available data to calculate setback and equipment failures. To assist with this we have the random module, activated by import random .
The module is used to generate random outputs in various degrees allowing you to run simulations or adding random elements to your calculations/exercises. For our purposes we will be using the following functions:
random.seed(): Sets the seed for random generation, takes one argument being the seed number you’d like to use. This means any random function used with any given seed will return the same results when used. This is used to exert some control over random generation and be able to recreate certain results.random.random(): Returns a random float point between0and1.random.randint(): Takes two(2) arguments, generates a number between the two arguments, including the numbers themselves. The result is returned in whole numbers/integer.random.uniform(): Same as the previous function but returns a random float point number instead of an integer.random.triangular(): Same as the previous function but has an extra argument as a modifier called mode. The mode needs to be a number between the two numbers in the random range. The random number generated will be a number statistically closer to the mode rather than a purely random number between the first two(2) arguments.random.choice(): Takes list as an argument, picks one (1) item from the list at random.random.choices(): Generates a random list sampled from another list, takes three(3) arguments: a list you would like to sample from, another list you can outline informing how much weight you would like to sample from each item in the list you’re sampling from and finally a variable named k you can assign a value to which outlines how many samples you would like to take from the list. The number of samples can be longer than the list itself as it will generate duplicates based on the weights you assigned.random.shuffle(): Takes a list as an argument and shuffles it randomly.random.sample(): Takes two arguments, a list to sample from and the number of samples you would like to take. A simplified version ofrandom.choices().
To start off walk to the light X mark and face the desk, use the read() function to read the memo and identify data points needed to run a simulation with some random variance. We’re going to run a simulation of equipment failure, the memo will inform you of what seed to run for the random generation as well as the list it’s sampling from. Use the random.seed() function and insert the value provided in the memo.
Walk to the dark X mark over the blue carpet and face the desk, we have a list constant named sectors which contains the areas of the farmland outlined in the previous memo. Create a variable named selection to store the value of random.choice(), use the sectors list constants to retrieve a sample from the list, like this: selection = random.choice(sectors) . Create another variable named delay and store the value of random.random() to simulate margin of errors caused by delays.
Make one more variable named defect and store the value of random.randint() to generate a percentage of potential equipment failure. Add as arguments 0 and 15 to generate a percentage between these two numbers. Once the: selection , delay and defect variables have been set up use the pre-written write() function with the three(3) variables in that order, to chart down data points that can be used to test for potential problems in the construction.
Walk to the gold X mark and use the read() function to continue running projections, the data in the memo will contain materials, quantities and sampling data. We’re going to run a larger simulation using the data in the memo, including a list constant named materials which stores the material types outlined in the memo.
Walk to the dark X mark over the red carpet and face the desk. Create a list named weights and store the four(4) number values present in the memo corresponding to the materials in the order they’re presented. For example: weights = [400, 600 , -insert value-, -insert value-]
Create a variable named simulation and store the value of random.choices() , add the materials list constant, the weights list you created and a list named k holing the sampling units outlined in the memo, like this: simulation = random.choices(materials, weights , k = insert sampling units ) . Use the simulation variable with the pre-written write() function to chart down the data.
Walk to the dark X mark over the green carpet and face the desk, use the random.shuffle() function with the simulation list you created to shuffle it, like this: random.shuffle(simulation) . This will shuffle the list, no need to store the value since it modifies the list directly. Create a list named sample to store the value of random.sample() , add the simulation list as the first argument and for the second argument, take a sampling of 3 to take three(3) random items as a sample, like this: sample = random.sample(simulation, k=3) . Use simulation and sample with the pre-written write() function to chart down the new samples.
Finally walk to the dark X mark over the purple carpet and face the desk, create a variable named defect and store the result of random.uniform() , add the arguments 3 and 10 . These are the sampling values for the original simulation and the sample taken from it, we’ll use this to generate potential equipment defect margins from the sample we took.
Create a variable named losses and store the value of random.triangular() , add the values 200 , 600 and 400 . These are the material numbers outlined in the previous memo, specifically the lowest, highest and the value closest to the middle. This allows us to get a triangular random value based on the number of materials the simulation used. Use the defect and losses values with the pre-written write() function to get results and complete the level.