Send to a Friend

What does this short snippet of C code do? (see details)
I have a short piece of C code that I am having trouble understanding (I don’t know much about C).
My best guess for what it does is that it opens a random number generator to make a random number “val” inside of a range given by “n”.
handle = open(”/dev/urandom”, O_RDONLY);
do {
read(handle, &bits, sizeof bits);
bits &= 0×7fffffff;
val = bits % n;
} while (bits – val + (n-1) < 0);
close(handle);
What I am looking for is a line-by-line, maybe even a word-by-word analysis of what it does and why, because I don’t have a clue what some of it does. I pretty sure that the answer is very straightforward, it is just that I don’t know what it is.
Using Fluther
or