Question: Exercise 2A # * Import the math library. # * Using the math library, calculate and print the value of pi / 4. # Write

Exercise 2A # * Import the math library. # * Using the math library, calculate and print the value of pi / 4. # Write your code for 1a here!

# Exercise 2B # * Add to the imports section random package # * Using random.uniform, create a function rand() that generates # a single float between -1 and 1. # Call rand() once. So we can check your solution, # I will use random.seed to fix the value called by your function.

random.seed(1) # This line fixes the value called by your function, # and is used for answer-checking.

def rand(): # define `rand` here!

rand()

# Exercise 2C # * The distance between two points (represented as touples of two numbers) # x and y is the square root of the sum of squared differences # along each dimension of x and y. Create a function distance(x, y) # that takes two vectors and outputs the distance between them. # Use your function to find the distance between x=(0,0) and y=(1,1). # * Print your answer.

def distance(x, y): # define your function here!

# Exercise 2D # * Write a function in_circle(x, origin) that determines whether a # two-dimensional point falls within a unit circle surrounding a given # origin. Your function should return a boolean that is # True if the distance between x and origin is less than 1, # and False otherwise. # * Use your function to determine whether the point (1,1) lies # within the unit circle centered at (0,0). # * Print your answer.

def in_circle(x, origin = [0]*2): # Define your function here!

# Exercise 2E # * Create a list of R=10000 booleans called inside that # determines whether each point in x falls within the unit circle # centered at (0,0). Make sure to use in_circle. # * Find the proportion of points within the circle by summing the # count of True in inside, and dividing by R. # * Print your answer. This proportion is an estimate of the ratio of # the two areas!

R = 10000 x = [] inside = [] for i in range(R): point = [rand(), rand()] x.append(point) # Enter your code here! #

# Enter your code here! #

# Exercise 2F # * Find the difference between your estimate from part 2e and math.pi / 4. # * Print your answer.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!