lundi 15 mars 2021

How do you print how many times a certain parameter was met in an if else statement?

As you can see below, I have a Monte Carlo calculation using M = 1000 so printing whether a step was left or right results in a very long list. Instead I’d like to print “Right = x” and “Left = y ” x and y being the amount of times the Walker stepped left and right.

I do not know how to go about that. Some help would be appreciated, Thank you!!

Here’s what i have so far


def Random_Walker(N, p, s):
  
    x = 0 # Walker starts from rest 
    
    for i in range(N): 
        if(np.random.rand() < p):
            print("right", count)
            x = x + s
        else: 
            print("left")
            x = x - s
            
    return(x)

# Defining the main part of the code 
def main():
    import numpy as np
    
    # Defining variables 
    N = 50 # Number of steps before stopping 
    p = 0.66 # Probability of stepping right 
    s = 0.75 # Length of a step in meters 
    
    M = 1000 # Number of walks
    
    X = np.zeros(M)
    for i in range(M):
        X[i] = Random_Walker(N, p, s)
        
    print(X)
    print("Mean = ", np.mean(X))
    print("Standard Deviation = ", np.std(X))
     
main()

Aucun commentaire:

Enregistrer un commentaire