samedi 9 avril 2022

Can I predict the value of Pi(3.14) using a random number function generated from scratch?

I have tried to predict the value of Pi using a random function generated from scratch. But it is giving an output of 4 instead of 3.14.My code for predicting Pi works perfectly when I'm using "np.random.uniform" instead of my random function.How can I improve my random function so that I can get an output of 3.14?

import numpy as np
import matplotlib.pyplot as plt
#random function using linear congruent generator
def generate_rand(mult=16807,mod=(2**31)-1, seed=123456789, size=1):
    U = np.zeros(size)
    X = (seed*mult+1)%mod 
    U[0] = X/mod 
    for i in range(1, size):
        X = (X*mult+1)%mod
        U[i] = X/mod 
    return U
def generate_random(low=0,high=1, seed=123456789, size=1):
#Generates uniformly random number between 'low' and 'high' limits
    return low+(high-low) *generate_rand(seed=seed, size=size)
def pi_estimator(samples):
    points_inside_circle= 0
    total_num_points = 0

    for _ in range(samples):
        x = generate_random()
        y = generate_random()
        distance = x**2 + y**2
        if distance <= 1:
            points_inside_circle +=1
        total_num_points += 1
    return 4* points_inside_circle/total_num_points
pi_estimator(10000)



Aucun commentaire:

Enregistrer un commentaire