function matconnect=Mathasard(NPN, N, Kex, Kin, Ferex, Ferin,Vex, Vin,choixhasard)
%BioSystems 89(2007) 244-256
%G.Horcholle-Bossavit et al.
%random connection matrix
global seedscalmat memoirej nappelbontir

x=clock;
x=100*(x(6));
rand('seed',x);

if choixhasard==1
     seedscalmat=rand('seed');
else
     rand('seed',seedscalmat)
     seedscalmat=rand('seed');
end

matconnect=zeros(N); 

for j=1:NPN
    memoirej=[0,j];
    for k=1:Kex 
        nappelbontir=0;
        i=Nbontirage(N);
        matconnect(i,j)=matconnect(i,j)+Vex;
    end
end

for j=NPN+1:N
    memoirej=[0,j];
    for k=1:Kin
        nappelbontir=0;
        i=Nbontirage(N);
        matconnect(i,j)=matconnect(i,j)-Vin;     
    end
end

for j=1:NPN
    memoirej=[0];
    vect=matconnect(:,j);
    vect=find(vect~=0);
    for f=1:Ferex 
        nappelbontir=0;
        e=Nbontirage(Kex);
        matconnect(vect(e),j)=matconnect(vect(e),j)-Vin-Vex;     
    end
end

for j=NPN+1:N
    memoirej=[0];
    vect=matconnect(:,j);
    vect=find(vect~=0);
    for f=1:Ferin
        nappelbontir=0;
        e=Nbontirage(Kin);
        matconnect(vect(e),j)=matconnect(vect(e),j)+Vin+Vex;     
    end
end