Example of Metropolis and Markov Chain Monte Carlo. Create a list of numbers that sample 
the Gaussian PDF with sigma = 1 and mean = 0;

Ntrials = 250000; 
xg =  Table[0,{i,Ntrials}]; 
xg[[1]]=0.2;  (* just an initial guess *)  
mypdf[x_]:= Exp[-x^2/2];  (* we just need a function that is proportional to the TARGET PDF*) 
For[i=1,i< Ntrials, i++, (* go through all trials *)  
 xnew = xg[[i]]+(RandomReal[] -0.5 )*0.5 ;  (*get a new number within +- 0.25 of the previous.  Try it with a smaller or larger number *)  
  (*xnew = xg[[i]]+RandomVariate[NormalDistribution[0,1]]*0.05; *)(* Try it this way. It does not matter what the initial PDF is  *)  
If [ RandomReal[]<Min[1,mypdf[xnew]/mypdf[xg[[i]]]],
xg[[i+1]]=xnew, xg[[i+1]]=xg[[i]]
]
]

It is interesting to understand how this Markov chain behaves with the “proposed” PDF. If the proposed PDF is too narrow then
the chain cannot sample the target distribution fast enough.  At infinity number of samples everything is o.k..

Histogram[xg,{-5,5,0.2}]

Mean[xg]

StandardDeviation[xg]

How about a Markov chain for sampling an exponential distribution.  Now this is not symmetric and so we must include that the PDF is zero for negative values.

Ntrials = 250000; 
xg =  Table[0,{i,Ntrials}]; 
xg[[1]]=1.0;  (* just an initial guess *)  
mypdf[x_]:= Exp[-x]*UnitStep[x];  (* we just need a function that is proportional to the TARGET PDF. UnitStep makes it zero for negative values  *) 
For[i=1,i< Ntrials, i++, (* go through all trials *)  
 xnew = xg[[i]]+(RandomReal[] -0.5 )*0.5 ;  (*get a new number within +- 0.05 of the previous.  Try it with a smaller or larger number *)  
If [ RandomReal[]<Min[1,mypdf[xnew]/mypdf[xg[[i]]]],
xg[[i+1]]=xnew, xg[[i+1]]=xg[[i]]
]
]

Histogram[xg,{-1,5,0.2}]