import matplotlib.pyplot as plt
import numpy as np  
from random import random
from numpy import linspace

# MONTE-CARLO  MONTE-CARLO  MONTE-CARLO  MONTE-CARLO 
# Q_16 et Q_17 #########################################☺
def alea(a):
    aux=-a+2*random()*a
    return aux


L=[alea(4) for i in range(100)]
L.sort()
print(L)
               


def A(n):
    c=0
    for i in range(n):
        x=alea(4)
        y=alea(3)
        if x**2/9+y**2/4<1:
            c=c+1
    return 48*c/n  #aire/aire du rectangle  = c/n

#Pour visualiser l'ellispe et les points 
def MC(n): 
    plt.clf()
    T=np.linspace(-np.pi,np.pi,200)
    xt=np.cos(T)*(3)
    yt=np.sin(T)*(2)
    cerclext=np.cos(T)
    cercleyt=np.sin(T)

    x=n*[0]
    y=n*[0]
    
    nb=0
    Dedans_x=[]
    Dedans_y=[]
    Dehors_x=[]
    Dehors_y=[]
    for i in range(n):
        x[i]=alea(4)
        y[i]=alea(3)
        if x[i]**2/9+y[i]**2/4<1:
            nb+=1
            Dedans_x.append([x[i]])
            Dedans_y.append([y[i]])
            
        else:
            Dehors_x.append([x[i]])
            Dehors_y.append([y[i]])
            
    plt.scatter(Dedans_x,Dedans_y,s=1,color='black') #s donne la grosseur des points
    plt.scatter(Dehors_x,Dehors_y,s=1,color='g') #s donne la grosseur des points    
    plt.title('Ellipse avec '+str(n)+' points')    
    plt.xlabel('x')
    plt.ylabel('y')
    plt.axis('equal') 
    plt.plot(xt,yt,linewidth=1,color='red')
    
    
    print('Valeur approchée (MonteCarlo) avec ',n,'points : ',
    nb/n*48)
    print('Valeur exacte (avec les intégrales doubles) : ',6*np.pi)
    plt.show()





# TENNIS - TENNIS - TENNIS - TENNIS - TENNIS - 
# Q_18..Q_20 #########################################☺
def  ber(p):   #tirage de Bernoulli  b(p)=B(p)
    tirage=random()
    if tirage<p:
        return 1
    else:
        return 0

def jeu(p): #renvoie 1 si 'A' gagne le jeu et 0 si 'B' gagne
    points_A=0;points_B=0
    for i in range(6):
        aux=ber(p)
        if aux==1:
            points_A +=1
        else:
            points_B +=1
        if points_A==4:
            return(1)
        if points_B==4:
            return(0)
    while abs(points_B-points_A)<=1:
        aux=ber(p)
        if aux==1:
            points_A +=1
        else:
            points_B +=1
     #   print(points_A,points_B)
    #print('jeu')
    if points_A>points_B:
        return(1)
    else:
        return(0)
        
      
# Q_12 #########################################☺
def f(p,nb): # renvoi statistiquement la proba. de  jeu(p)
    c=0
    for i in range(nb):
        c=c+jeu(p)
    return(c/nb)

def fT(p): # valeur de la probabilité exacte de jeu(p)
    return (15*p**4-34*p**5+28*p**6-8*p**7)/(1-2*p+2*p**2)

# Q_13 #########################################☺
def traceTennis():
    LX=linspace(0,1,500)
    LY=[f(p,5000) for p in LX]
    plt.plot(LX,LY)
    plt.show()
   
   



#close() sinon pas de dessin

# Q_22#########################################☺

def trace(): #trace l'hyperboloide à une nappe
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    u = np.linspace(0, 2 * np.pi, 100)
    v = np.linspace(-1,1, 100)

    x = np.outer(np.cos(u), np.cosh(v))
    y = np.outer(np.sin(u), np.cosh(v))
    z = np.outer(np.ones(np.size(u)), np.sinh(v))
    
    ax.plot_surface(x, y, z,  rstride=4, cstride=4, color='g')
    plt.show()

cube=4*4*2;

# Q_23 #########################################☺
def al(b): # genere un nb alea entre -b et b
    return -b+2*b*random()

# Q_24 #########################################☺
def montecarlo(n):
    nbdedans=0;nbdehors=0;
    for i in range(n):
        x=al(2);y=al(2);z=al(1);
        if (x**2+y**2-z**2)<=1 and z**2<=1:
            nbdedans=nbdedans+1;
        else:
            nbdehors=nbdehors+1;
    return(cube*nbdedans/n,nbdedans,nbdehors)
 
# Q_25 #########################################☺
def vol(nb,max):
    s=0
    for i in range(nb):
        s=s+montecarlo(max)[0]
    return s/nb

# Valeur exacte avec les intégrales triples : 8*Pi/3
