[Python] Running trials in python

Discussão em 'Python' iniciado por Stack, Fevereiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I'm a beginner when it comes to using Python and wants to do simulations where I estimate pi using trials. I first important random numbers for both the x and y values (between 0 and 1), then I check to see if those numbers are inside the circle. If they are then I estimate pi using the formula: (4 * (number of points inside circle/number of trials)). My code is attached below. With the seed I'm using I should get a number around 3.02 but instead, am getting 4.00. Could anyone point in the direction of where I've gone wrong?

    import random

    random.seed(1000)


    x_value = random.random()

    y_value = random.random()

    print(x_value)
    print(y_value)

    pointsInCircle= 0
    numberOfTrials = 1000

    for trials in range(trials):
    if 1 > ((x_value ** 2) + (y_value ** 2)):
    pointsInCircle = pointsInCircle + 1
    else:
    print("No estimation possbile")

    pi = 4 * (pointsInCircle/numberOfTrials)

    print(pi)

    Continue reading...

Compartilhe esta Página