Because of the pigeon hole principle, you can't just use
output = min + (rand() % (int)(max - min + 1))
to generate an unbiased, uniform, result. This answer to a similar question provides one solution but it is highly wasteful in terms of random bits consumed.
For example, if the random range of the source is low, then the chances of having to generate a second value from the source can be quite high. Alternative, using a larger source range is inherently wasteful too.
While I'm sure an optimal source range size could be derived, that doesn't address the question that there may be a better algorithm, rather than optimizing this one.
[EDIT] The my idea has been shown in the answers to produce biased results.
An approach that occurs to me is
- Consume the minimum number of bits necessary to cover the desired range.
- If that value is outside the desired range only throw away one bit and consume one more.
- Repeat as necessary.