That's correct, there is no function in ANSI C that returns a random number within a custom range.
Bump! That sucks. Man i love the borland guys. They make such usefull stuff.
Actually that doesn't suck at all. You can write your own trivial wrapper function which fits your needs exactly. It may be arbitrarily precise (like the above one) or abitrarily efficient (like for example using
& if you need a less-than-some-power-of-two random).
For example, the above function is guaranteed to return a good random distribution. However, other solutions are possible which are on the order of 2-10 times faster but which might not have the same statistical properties (
rand % a + b will for example be about twice as fast, and a construct using
& instead of
% might be 10-20 times as fast).
For applications which require a good random distribution, using
% and
& was an extremely bad idea back in the old days (1970-2000), as in the good old standard C generator the lower bits were not random at all (and
% isn't either, if you are pedantic)!
Practically every
rand() implementation uses MT these days, and most applications don't need perfect distributions, so that is fine either way...
If you use whatever the Borland guys made up, then you are bound to whatever they decided was right. This may not be what you want.