I found function rand(), it can create random number but this
function can not define the range of number which I want to get
it, such as, I want to get random number in the range from 0 to
100 [0-100].
See the FAQ, question 13.16.
The FAQ is deceptive and incomplete on this issue
How so?
It gives an "answer", that is insufficient, and accompanied by
incorrect explanations.
That's answered in question 13.21, which question 13.16 directly
refers to.
Right, and its off topic for this newsgroup, right? I mean, what is
it even doing in the FAQ?
You raise 3 major points on that page.
1. If the desired range does not divide the range of values returned
by rand() a bias is introduced. FAQ 13.16 directly addresses that.
The FAQ suggests that if the range is small enough, then you are ok.
This is untrue if accuracy is required, regardless of of what the
range is. Specifically, if you take 1000 * (RAND_MAX / RANGE)
samples, you will see a statistically significant deviation from the
samples from the techniques they suggest and from a proper evenly
distributed random sample. This is tantamount to the FAQ saying
"don't sample this too much". On modern machines, given that RAND_MAX
is commonly 32767, that number of samples is going to pretty much
always be "smallish".
2. The quality of rand() is often not very good. FAQ 13.16 also
directly addresses that.
It addresses the wrong things. In order to get more bits out of it,
you have to call it a successive number of times, however, rand()
implementations with too small of a state cannot produce all possible
permutations of successive calls -- in fact successive calls are
typically deterministic after 32 bits are output. For this reason, to
be ideal, you need to use a PRNG that has a large enough state (such
as the Mersenne Twister) to avoid this issue.
If you know anything about computer graphics; using a small state
rand() successively is like using (stochastic) dithering for images,
while using a large state rand() is like having truly higher
resolution. Having the higher resolution is generally better than
dithering which is basically trying to gloss over the fundamental
weakness of having lower resolution.
3. There are problems if the desired range is wider than RAND_MAX. I
think that's a valid criticism, though it certainly doesn't justify
using the word "deceptive".
It said there are problems if N is close to RAND_MAX (they should
point out that its devastatingly wrong if RAND_MAX > N > RAND_MAX/2),
which includes N being slightly larger than RAND_MAX. If it said "if
N approaches RAND_MAX from below" or something like that you could
almost forgive them, but they didn't.
[...] (And in this particular context, the OP wanted range of 0..100,
and RAND_MAX is guaranteed to be at least 32767.)
Give a man a fish vs teaching him to fish ...
Your page appears to contain a lot of good information, and I hope to
have the time to read the whole thing at some point, but it's probably
more than would be appropriate for the clc FAQ.
Well the FAQ could simply give my implementation of randbiased () and
randrange () (the two together are not prohibitively long) and say
that why it works is an exercise to the reader, or point to my page
about it.
[...] Adding a pointer to
your page to the "additional links" page
<
http://www.c-faq.com/lib/sd15.html> might be a good idea.
Looks like more half-answer kind of things going on in there. It kind
of reminds me of being forced to vote for the democratic nominee or
the republican nominee instead of the candidate you actually want for
the office.
Sometimes the right answer matters.
Perhaps the FAQ has been updated since the last time you read it?
It was, but insufficiently.