Skip to content
Snippets Groups Projects

Monte-Carlo Circle Surface Area

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Marvin Jütte

    Code used to generate images for proseminar presentation

    Edited
    monte-carlo.py 805 B
    import numpy as np
    import matplotlib.pyplot as plt
    
    n = 1500
    x = 1-2*np.random.random(int(n))
    y = 1-2*np.random.random(int(n))
    insideX, insideY = x[(x*x + y*y) <= 1], y[(x*x + y*y) <= 1]
    outsideX, outsideY = x[(x*x + y*y) > 1], y[(x*x + y*y) > 1]
    
    circle = plt.Circle((0, 0), 1, color='orange', fill=False)
    
    fig, ax = plt.subplots(1)
    ax.set_xlim(-1.1, 1.1)
    ax.set_ylim(-1.1, 1.1)
    ax.set_aspect('equal', 'box')
    
    ax.add_patch(circle)
    
    plt.savefig('circle-raw.png', bbox_inches='tight')
    
    ax.scatter(insideX, insideY, color= 'black')
    ax.scatter(outsideX, outsideY, color= 'black')
    
    plt.savefig('circle-black.png', bbox_inches='tight')
    
    ax.scatter(insideX, insideY, color= 'red')
    ax.scatter(outsideX, outsideY, color= 'blue')
    plt.savefig('circle-classified.png', bbox_inches='tight')
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment