# 4.5. Exercises¶

Use a

`for`

statement to print 10 random numbers.Repeat the above exercise but this time print 10 random numbers between 25 and 35.

## 4.5.1. Contributed Exercises¶

Q-1: After completing the reading, what concepts are still unclear to you? If nothing is unclear, what did you find most interesting?

- prob = random.randrange(1, 101)
- This will generate a number between 1 and 101, but does not include 101.
- prob = random.randrange(1, 100)
- This will generate a number between 1 and 100, but does not include 100. The highest value generated will be 99.
- prob = random.randrange(0, 101)
- This will generate a number between 0 and 100. The lowest value generated is 0. The highest value generated will be 100.
- prob = random.randrange(0, 100)
- This will generate a number between 0 and 100, but does not include 100. The lowest value generated is 0 and the highest value generated will be 99.

Q-1: (TEST) The correct code to generate a random number between 1 and 100 (inclusive) is:

In statistical physics, it is common to use Stirling’s approximation for \(N!\),

Obtain an integer from the user, assign it to `N`

, and print out, in order,
\(N!\), Stirling’s approximation, and the relative percent
error. The relative percent error is calculated as

For a right triangle we have

Compute and print \(\theta\) for

Hint: use the function `atan2()`

instead of `atan()`

.

Use the double angle formula,

to compute and print

- import math
- Correct! This will make it obvious to anyone who reads your code when you use something from the ``math`` module.
- from math import exp
- This imports only the ``exp()`` function and puts it in the global namespace. This can make it difficult to tell where this function comes from.
- from math import *
- This imports everything from ``math`` and puts it in the global namespace. This can make it difficult to tell when the ``math`` module is being used.
- import math as m
- Don't change the names of modules unless it is the convention for that module. This makes your code harder to read.

Q-1: Which is the best way to import the `math`

module so you can use the `exp()`

function?

`exp()`

`abs()`

`int()`

`sin()`

`fabs()`

`pi`

Q-1: Which of the following functions are part of the `math`

module?

Write a program to convert square km to square meters.

In mathematics, the Leibniz formula for π, named after Gottfried Leibniz, states that

Write a program to calculate π when n is 99 and 999. When n becomes bigger, does π becomes more accurate?

Write a program to print an n*n times table. n could be any number. For example, the following is a 5*5 times table:

1 x 1 = 1

1 x 2 = 2 2 x 2 = 4

1 x 3 = 3 2 x 3 = 6 3 x 3 = 9

1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16

1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25

Write a Python program to print 10 random integers between 1 and 100, inclusive. Print one random integer per line. Use a loop. I have started the program for you.

- prob = random.randrange(1, 101)
- This will generate a number between 1 and 101, but does not include 101.
- prob = random.randrange(1, 100)
- This will generate a number between 1 and 100, but does not include 100. The highest value generated will be 99.
- prob = random.randrange(0, 101)
- This will generate a number between 0 and 100. The lowest value generated is 0. The highest value generated will be 100.
- prob = random.randrange(0, 100)
- This will generate a number between 0 and 100, but does not include 100. The lowest value generated is 0 and the highest value generated will be 99.

Q-1: (TEST) The correct code to generate a random number between 1 and 100 (inclusive) is:

- A program that reads in a data file of historical rainfall statistics, and predicts future rainfall as the average of the past 10 years' rainfall statistics.
- "Deterministic" means if the program is run with the same inputs, it will always produce the same output.
- A program that asks the user to think of a number from 1 - 100. The program tries to find the user's number through bisection search, repeatedly outputting a guess and asking the user to input whether the guess is correct, too high, or too low.
- "Deterministic" means if the program is run with the same inputs, it will always produce the same output.
- A program that picks a secret number randomly, then asks the user to guess the number. At each step, the computer outputs whether the guess is correct, too high, or too low.
- Since this program includes a random number being generated, the results will not always be the same, even given the same inputs.
- A program that asks the user to input a number, then computes the square root of the number using the Newton-Raphson method.
- "Deterministic" means if the program is run with the same inputs, it will always produce the same output.

Q-1: Which of the following is NOT deterministic?

- 0.5
- It is 1 minus the probability of getting no 3's, so 1 - (5/6)^5.
- 0.6
- It is 1 minus the probability of getting no 3's, so 1 - (5/6)^5.
- 0.7
- It is 1 minus the probability of getting no 3's, so 1 - (5/6)^5.
- 0.8
- It is 1 minus the probability of getting no 3's, so 1 - (5/6)^5.

Q-1: What is the approximate probability of rolling a 3 **at least once** in 5 die rolls?

- 0.2
- In a single die roll, the probability of NOT getting a 3 is 5/6. Each die roll is independent, so the probability of not getting a 3 in each of the 5 die rolls is (5/6)^5.
- 0.3
- In a single die roll, the probability of NOT getting a 3 is 5/6. Each die roll is independent, so the probability of not getting a 3 in each of the 5 die rolls is (5/6)^5.
- 0.4
- In a single die roll, the probability of NOT getting a 3 is 5/6. Each die roll is independent, so the probability of not getting a 3 in each of the 5 die rolls is (5/6)^5.
- 0.5

Q-1: What is the approximate probability of rolling a die 5 times without getting a single 3?

- 55%
- 68%
- 79%
- 95%

Q-1: Given normally distributed data on a random variable of interest, approximately what percentage of the data falls within one standard deviation of the mean?