Why compare with uniform distributed values in metropolis-hastings? I'm a new starter in metropolis-hastings algorithm, having a problem in understanding its implementation of acceptance step:
  min{1, f(Y)/f(x)}

I understand when acceptance rate is larger than 1, the value is always accepted. But when the ratio is less than one, all the R code I have see is to compare with a uniform density value by
if(runif(1)<f(Y)/f(x))

Why are we supposed to compare with one uniform density value here? Is any specific reason for that?
 A: *

*You need a way to decide to randomly generate a move with the required probability (i.e. to "move with probability $\alpha$")

*computers have good ways to generate uniformly distributed random numbers (rather, pseudo-random, with characteristics that give us what we need).

*by comparing a standard uniform random number with $\alpha$ we can say "move if $u<\alpha$" to take the proposal with the required probability (otherwise remaining where we are).
A: If I understand you correctly, you seem to be asking why uniform distribution is helpful in Metropolis algorithm? Why do we use it to make non-uniform random draws? Actually the answer is that we use it because we can!
When laypeople talk about randomness, by "random" they usually mean drawn uniformly at random. Uniform sampling is such popular because it is most easy thing to do. Instead of discussing the Metropolis algorithm, let me focus on the most beloved probabilistic example: the biased coin. Imagine that you wan to sample outcomes from Bernoulli distribution (biased coin) with probability of success (heads) equal to $p$ and of failure (tails) equal to $1-p$. The first problem that you'd encounter is that biased coin is a physical impossibility, so you simply cannot mint it and throw a number of times. But you don't need the coin, instead you can produce $N$ coupons, write "heads" on $pN$ of them and "tails" on $(1-p)N$ of them, put them into box, shuffle and then draw uniformly with replacement. Since there will be $pN$ "heads" coupons in the box, you can expect to draw "heads" with probability $p$. This sampling schema can be easily extended to random variables with more then two outcomes, but with continuous random variables you'd need infinite number of coupons, so it would be practically impossible. It shows however that you can draw from a discrete non-uniform distribution using discrete uniform random number generator as a proxy.
What about sampling from continuous random variables? Actually the example above can be easily extended to continuous case and we can make it more general. Imagine that instead of coupons you produce a wheel of fortune divided into "heads" and "tails" areas, where length of the external border of the "heads" area is equal to $p$ and the reminder has $1-p$ length (see the ugly picture below).

If you spin this wheel, it will stop either on the "heads", or "tails" area with probabilities $p$ and $1-p$ what follows from the properties of continuous uniform distribution. Of course, instead of dividing the area of the wheel into two areas, you can divide it into any (possibly infinite up to measurement precision) number of them. Instead of dividing your wheel of fortune into a number smaller areas, you can instead divide it into two areas to simulate the biased coin and if you need to make a decision if you accept some outcome $x$ that appears with probability $p = f(x)$, you'll spin your wheel and let it decide -- in the end you'll accept $x$ with probability $p$ and reject it with probability $1-p$. This means that you can use such wheel of fortune for sampling from any distribution (it doesn't mean that this will be the most efficient way to go). 
This is helpful not only for physical sampling games, but also for computer-based sampling. We have a number of uniform pseudorandom number generators and sampling uniform numbers is usually easier then sampling from other distributions, so it's a good starting point. You can also read about inverse transform sampling for other application of this idea. It applies also to Metropolis algorithm and to most of other similar algorithms. 
A: Going off of @jbowman's comment. It's a way for a computer to make a decision with some probability. The computer needs a decision rule and since we want it to move with the probability given by the ratio, a quick if(runif(1)<f(Y)/f(x)) (as you said) will make that decision with the correct probability.
