Reflection / Notes

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. = Answer

C. A random word from variable a to variable b exclusive.

D. A random word from variable a to variable b inclusive.

2. What is x, y, and z in random.randrange(x, y, z)?

A. x = start, y = stop, z = step = Answer

B. x = start, y = step, z = stop

C. x = stop, y = start, z = step

D. x = step, y = start, z = stop

3. Which of the following is NOT part of the random library?

A. random.item = Answer

B. random.random

C. random.shuffle

D. random.randint

4

  1. What is the advantage of using libraries?
  1. Write a thorough documentation of the following code.

5

  1. Create a program to pick five random names from a list of at least 15 names

import random

names = [“jerry”, “Bob”, “Charlie”, “David”, “Emily”, “Frank”, “Gina”, “Hannah”, “Ivan”, “Jenny”, “Karen”, “Liam”, “Megan”, “Nina”, “Owen”]

selected_names = random.sample(names, 5)

print(“The selected names are:”) for name in selected_names: print(name)

import random

player1_dice1 = random.randint(1, 6) player1_dice2 = random.randint(1, 6) player1_sum = player1_dice1 + player1_dice2

player2_dice1 = random.randint(1, 6) player2_dice2 = random.randint(1, 6) player2_sum = player2_dice1 + player2_dice2

if player1_sum > player2_sum: print(“Player 1 wins!”) elif player2_sum > player1_sum: print(“Player 2 wins!”) else: print(“It’s a tie!”)