Unit 3 Lesson 14-15

Notes

Procedures found in software libraries are utilized to create new programs.

The creation of algorithms can be greatly simplified by libraries and prewritten code.

Libraries make complex programs simpler.

Application program interfaces (APIs) define how library procedures should act and be used.

Reading the documentation makes it much simpler to understand how to use libraries and APIs.

Uses of the random function include:

#1 Creates a random number using random.randint(a, b). As the start and finish values, a and b’s values are necessary in order to set an inclusive limit on the number that can be generated. The optional step C determines the interval at which random numbers are created.

#2 A random integer is produced using the function random.randrange(a, b, c). As the start and finish values, a and b’s values are necessary in order to set an inclusive limit on the number that can be generated. The step that explains the type of multiple that can be formed is C. The potential value is less than b and begins at an or is a multiple of c plus a multiple of a.

#3 The function random.shuffle randomly shuffles a list’s contents.

Hacks : MC Questions

What does the random(a,b) function generate? A. A random integer from a to be exclusive B. A random integer from a to b inclusive. C. A random word from variable a to variable b exclusive. D. A random word from variable a to variable b inclusive.

Answer: B, it contains the endpoints and all of the intermediate numbers

What is x, y, and z in random.randrange(x, y, z)? A. x = start, y = stop, z = step B. x = start, y = step, z = stop C. x = stop, y = start, z = step D. x = step, y = start, z = stop

Answer: A, the order is start, stop, step

Which of the following is NOT part of the random library? A. random.item B. random.random C. random.shuffle D. random.randint

Answer: A, the random.item function does not exist. However, random.random generates a random floating-point number between 0 and 1. random.shuffle can be used to randomly rearrange a list, and random.randint produces a random integer.

Hacks : Free Response

#1 Libraries allow users to access and reuse pre-written code, which can improve the efficiency of algorithms. There are many libraries available for different applications, making it easier for programmers to build more complex algorithms.

#2 The code starts by importing the random library. It then prompts the user to input names, which are stored in a list called names. The variable num_items is used to determine the number of items in the list. The random_choice variable uses a random number generator to select an index from the names list. The random.randint function is used to generate a value from 0 to the maximum index of the list (which is the length of the list minus 1). The software then uses the generated value to identify the person associated with that index, and prints their name.

Coding Challenges

Extra Credit

Reflection

I have acquired knowledge about APIs, libraries, and documentation. As part of the AP Exam project, I need to document my code, explaining its functionality and implementation. Libraries are a valuable resource because they provide pre-written code that I can use and refer to in my own programs, making the development process more efficient. Randomization is a useful technique used in many programs, including games and webpages, to introduce variability and unpredictability.