//1 Histogramme loi binomiale
X=grand(1,1000,'bin',6,0.3);
Pbin=dbinom([0:6],6,0.3)
clf
histplot([-0.5:6.5],X)
plot2d3([0:6],Pbin,3)    


//4 Fonction de repartition empirique
clf
X=grand(1,1000,'nor',0,1);
Y=gsort(X,'g','i');
n=length(X);
plot2d2(Y,[1:n]/n);
t=[-3:0.1:3];
plot2d(t,pnorm(t),2)


//5 Uniformes
clf
X=rand(1,1000); 
Y=2*%pi*X;
Z=2*X-1;
histplot(20,Y)
histplot(30,Z,style=2)

//6 Bernoulli
Ber=1*(X<1/4);
mean(Ber)

//7 Binomiale
B1=(rand(1,10)<0.4);
Bin1=sum(B1);
Bin=zeros(1,100)
for i=1:100
    B1=(rand(1,10)<0.4);
    Bin(i)=sum(B1);
end

//8 Uniforme discrete
X=rand(1,1000);
U=ceil(6*X);

// 9 Loi discrete
X=rand(1,1000);
Y=1*(X<3/5)+ 2*((X>3/5)&(X<4/5))+ 3*(X>4/5);
clf
histplot([0.5:3.5],Y);
clf
Z=gsort(Y,'g','i');
n=length(Y);
plot2d2(Z,[1:n]/n);

//10 Voir discrete.sci

//11 Loi geometrique
Geom=zeros(1,1000);
for i = 1:1000
    X=rand(1);
    Y=1;
    while (X>1/6)
        Y=Y+1;
        X=rand(1);
    end
    Geom(i)=Y;
end;
mean(Geom) // a comparer a 6

// 12 Voir la fonction rpareto
Pareto=rpareto(1000,2);
t=[1:0.01:10];
Y=gsort(Pareto,'g','i');
clf
plot2d2(Y,[1:1000]/1000);
plot2d(t,1-t^(-2),2)


//13 Loi de Cauchy
Cauchy=rcauchy(1000);
t=[-20:0.01:20];
Y=gsort(Cauchy,'g','i');
clf
plot2d2(Y,[1:1000]/1000);
plot2d(t,1/%pi*(atan(t)+%pi/2),2)
mean(Cauchy) // Une v.a. suivant une loi de Cauchy n'est pas integrable

//  14 Uniforme sur l'ellipse
X=4*rand(10000,1)-2;
Y=2*rand(10000,1)-1;
I=find((X^2/4+Y^2)<1);
Xd=X(I); Yd=Y(I);
clf
plot2d(Xd,Yd,-1)
t=[0:%pi/30:2*%pi]; plot2d(2*cos(t),sin(t),2)
// I suit une loi binomiale de parametre 10000, 2pi/8 = pi/4
scf(2)
histplot(15,Xd,15);// Xd n'est pas uniforme

// 15
r=rand(10000,1);
t=2*%pi*rand(10000,1);
clf
plot2d(2*r.*cos(t),r.*sin(t),-1) // pas uniforme

// 16 Loi beta 
Max=20*27/4^4
X=rand(10000,1);
Y=Max*rand(10000,1);
J=find(Y<20*X.*(1-X)^3);
scf(2)
clf
plot2d(X(J),Y(J),-1)
t=[0:0.01:1];
plot2d(t,20*t.*(1-t)^3,2)
rbeta=X(J);
scf(3)
clf
histplot(10,rbeta);
plot2d(t,20*t.*(1-t)^3,2)

// 17 Loi de Poisson
X=zeros(1000,1);
for k=1:1000
    S=-log(rand(1))/2;
    while(S<1)
        S=S-log(rand(1))/2;
        X(k)=X(k)+1;
    end;
end
mean(X)
clf
histplot(10,X)
k=[0:10];
Pois=exp(-2)*2.^k./factorial(k);
plot2d3(k,Pois,2);
scf(4)
Y=gsort(X,'g','i');
plot2d2(Y,[1:1000]/1000);
plot2d2(k,cumsum(Pois),2)





// Fonctions du poly
U=rand(1)
if (U<0.3)  X=1; else X=0;
end
X

U=rand(1)
X=1*(U<0.3)

U=rand(1)
if(U<2/3) then X=1; 
    elseif (U<5/6)  then X=34; 
    else X=48;
end
X

U=rand(1)
X=(U<2/3)+34*(U>2/3 & U<5/6) + 48*(U>5/6)

U=rand(1)
X=ceil(3*U)

U=rand(1,10)
Y=U<0.4
X=sum(Y)