Reflection

I gained knowledge about APIs, libraries, and documentation. The AP Exam project portion includes documentation, which entails describing what and how your code does its thing. Because libraries are pre-written and you can reference them throughout your code, they serve to make coding more effective. In programs, randomization can be quite helpful. Many of the programs that we use, such games and webpages, use randomization.

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.

  • Among the helpful random functions are:

    • 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.

    • 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.

    • The function random.shuffle randomly shuffles a list's contents.

MC

  1. 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, since it contains the endpoints and all of those intermediate numbers

  1. 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

  1. 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 item random.item doesn't exist. Using random.random, a float between 0 and 1 is produced. The function random.shuffle randomly arranges a list. A random integer is produced using random.randint.

Short Answer Questions

1

Users can access and reuse pre-written code by using libraries. This enables the improvement of algorithms' efficiency. Additionally, there are numerous libraries that are developed for a variety of applications, which makes it simpler for programmers to construct algorithms with greater complexity.

2

The first line of code imports the random library. The software then gathers names from user input and compiles them into a list (names). The number of items in the list is then determined by the variable num items. Using a random number generator, the random choice variable creates an index for the list. The random.randint function is used by random choice to provide a value from 0 to the maximum index (length of list minus 1). Then the software identifies the individual who is connected to the value that was generated at random. The name of that person is then printed by the application.

Documentation: I assign the variable names string to the names the user enters using the random function in the library. The names will be separated using commas by the split function. Each name will be assigned a number by num items, which will then use the random function randint to choose a random number for each name. The procedure then returns the text and the name.

import random 

names_string = input("Give me everybody's names, seperated by a comma.")
names = names_string.split(",")

num_items = len(names)

random_choice = random.randint(0, num_items - 1)

person_who_will_pay = names[random_choice]

print(f"{person_who_will_pay} is going to buy the meal today!")
Riggi is going to buy the meal today!

Code

import random

names_list = ["Riggi", "Nico", "Drew", "Avery", "Jack", "Jaden", "Dean", "Cole", "Zion", "Hayden", "Nate", "Ryder", "Connor", "Ash", "Sanburg", "Ron Jay"]
random_list = []

def addPeople(list, new_list):
    count = 1
    while count <= 5:
        number = random.randint(0, len(list) - 1)
        new_list.append(list[number])
        count += 1
    return new_list

print(addPeople(names_list, random_list))
['Drew', 'Hayden', 'Sanburg', 'Nate', 'Jack']
import random
score1 = 0
score2 = 0

def DiceGame():
    score1 = random.randint(1, 6) + random.randint(1, 6)
    score2 = random.randint(1, 6) + random.randint(1, 6)
    if score1 > score2:
        print("Player 1 won game 1 with a score of " + str(score1) + " points!")
    if score1 < score2:
        print("Player 2 won game 2 with a score of " + str(score2) + " points!")
    if score1 == score2:
        print("Both players tied with " + str(score1) + " points!")

DiceGame()
DiceGame()
Player 1 won game 1 with a score of 6 points!
Both players tied with 9 points!
import random

# defining constraints
width = 5
height = 5
obstacles = 12

# creates the maze
maze = [[0 for i in range(width)] for j in range(height)]

# for loop to generate obstacles
for i in range(obstacles):
    x = random.randint(0, width - 1)
    y = random.randint(0, height - 1)
    maze[x][y] = 'x' # represents obstacles

# function to create a start and end position
def startEnd():
    a = random.randint(0, width - 1)
    b = random.randint(0, height - 1)
    maze[a][b] = 'S' # represents the start position
    c = random.randint(0, width - 1)
    d = random.randint(0, height - 1)
    maze[c][d] = 'E' # represents the end position

startEnd()

# function to print the maze
for row in maze:
    print(' '.join(str(cell) for cell in row))
x S 0 x 0
0 x 0 0 x
0 0 0 E x
0 x 0 0 x
0 0 0 x x