// Exercice 1

function res=pile_ou_face ()
    a = rand()
    res = -1
    if a> 0.5 then res = 1
    end
endfunction

function pos= position_1D(N)
    pos = zeros(N)
    for i=1:N-1
        pos(i+1) = pos(i) + pile_ou_face()    
    end
endfunction

function [posmin,posmax]= max_min (pos)
    posmin = min(pos)
    posmax = max(pos)
endfunction

function pos= position_1D_bis()
    pos = []
    l = 1
    posmin = 0
    posmax = 0
    while (l <10000) & (max(abs(posmin),abs(posmax))<100)
        pos(l+1) = pos(l) + pile_ou_face() 
        [posmin,posmax]= max_min (pos)
          l = l+1
    end
endfunction

// Exercice 2
function res=boussole ()
    a = rand()
    b = rand()
    res(1) = -1
    res(2) = -1
    if a> 0.5 then res(1) = 1
    end
     if b> 0.5 then res(2) = 1
    end
endfunction


function [X,Y]= position_2D(N)
    X = zeros(N)
    Y = zeros(N)
    for i=1:N-1
        res = boussole()
        X(i+1) = X(i) + res(1)  
         Y(i+1) = Y(i) + res(2)    
    end
endfunction


function dist= max_distance (X,Y)
    dist = sqrt(max(X.^2+Y.^2))
endfunction
