Imagine a Cartesian coordinate plane and a square of side length 2 centered at the…
- User
Imagine a Cartesian coordinate plane and a square of side length 2 centered at the origin. The square will have vertices at (-1, -1), (-1, 1), (1, 1), and (1, -1). Inside the square, a circle of radius 1 unit centered at origin as shown below. Figure 1: Monte-Carlo Simulation Now, we randomly generate a large number of points (x, y) within the square. The x and y coordinates of these points should be between -1 and 1. For each generated point, we check if the point falls within the circle (x2 + y 2 ≤ 1) and keep a count of no. of points falling inside the circle and total no. of random points generated. Are of the circle = π4 . We empirically estimate Now the P(random point lie in the circle) = Area of the square Are of the circle this probability by random sampling done above. Therefore, π = 4 × Area . of the square Update: Use Python to simulate the above process and compare the π value obtained using the pseudo-random number generator from the previous subtask and random.random() function from random module in python. Note: You must import the pseudo rand num gen(seed, k) function from part (a) .py file, instead of copy-pasting the function code.
- Assistant
- User
import random from q1_a import pseudo_rand_num_gen
def estimate_pi(num_samples, rng_function): inside_circle = 0 for _ in range(num_samples): # Generate a random point using the provided random number generator function x = rng_function() y = rng_function()
# Adjust the range from [0, 1) to [-1, 1) x = x * 2 - 1 y = y * 2 - 1 # Check if the point falls inside the unit circle if x**2 + y**2 <= 1: inside_circle += 1 # Estimate π pi_estimate = 4 * inside_circle / num_samples return pi_estimateDefine the number of samples
num_samples = 100000
Estimate π using Python’s built-in random.random()
pi_random = estimate_pi(num_samples, random.random) print(f"π estimate using random.random(): {pi_random}")
Define the pseudo-random number generator function (replace with your implementation)
def pseudo_rand_num_gen(seed, k): # Your implementation of pseudo-random number generator pass
Define the seed and k for the pseudo-random number generator, if required
seed = 123 # Example seed k = 10000 # Example k
Estimate π using the pseudo-random number generator
pseudo_rng = lambda: pseudo_rand_num_gen(seed, k) pi_pseudo_rng = estimate_pi(num_samples, pseudo_rng) print(f"π estimate using pseudo-random generator: {pi_pseudo_rng}")
- Assistant
- User
########################################################### Done ############################################################################ import time import matplotlib.pyplot as plt
def pseudo_rand_num_gen(seed=None, k=1): if seed is None: seed = int(time.time()) # Use current system time as seed
seed_str = str(seed) if len(seed_str) % 2 != 0: seed_str = '0' + seed_str n = len(seed_str) rand_nums = [] for _ in range(k): seed_squared = int(seed_str) ** 2 seed_squared_str = str(seed_squared).zfill(n * 2) middle_digits = int(seed_squared_str[(n // 2): (n // 2 + n)]) rand_nums.append(middle_digits) seed_str = str(middle_digits).zfill(n) normalized_nums = [num / (10 ** n) for num in rand_nums] plt.hist(normalized_nums, bins=max(10, k // 10), edgecolor='black') plt.title('Pseudo-Random Numbers Distribution') plt.xlabel('Number Range') plt.ylabel('Frequency') plt.show() return normalized_numsrandom_numbers = pseudo_rand_num_gen(k=100)
this is the already defined function
- Assistant
- User
Bonus: Calculate and plot the estimated error value in calculation of π for each iteration. You are allowed to use matplotlib library for plotting purposes.
- Assistant
- User
########################################################### Done ############################################################################
import random from q1_a import pseudo_rand_num_gen
def estimate_pi(num_samples, rng_function): inside_circle = 0 for _ in range(num_samples): x, y = rng_function(), rng_function() if x2 + y2 <= 1: inside_circle += 1 pi_estimate = 4 * inside_circle / num_samples return pi_estimate
num_samples = 100000 pi_random = estimate_pi(num_samples, lambda: random.uniform(-1, 1)) print(f"π estimate using random.random(): {pi_random}")
def pseudo_rand_num_gen(seed, k): random.seed(seed) for _ in range(k): yield random.random()
seed = 123 num_random_numbers = num_samples * 2 random_numbers = pseudo_rand_num_gen(seed=seed, k=num_random_numbers)
def next_pseudo_random(numbers): for num in numbers: yield num * 2 - 1
pseudo_random_generator = next_pseudo_random(random_numbers)
pi_pseudo_rng = estimate_pi(num_samples, lambda: (next(pseudo_random_generator))) print(f"π estimate using pseudo-random generator: {pi_pseudo_rng}")
why am i defining pseudo_ran_num again im already importing it from q1_a why do i need to defien again
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.