Range scaling is another approach here, and is what I would generally prefer when dealing with floating points:
- rand() returns a number between [0..RAND_MAX]
- knowing that, you can scale that down to a range between [0..1] by dividing by RAND_MAX
- then you can scale up to whatever range you want by multiplying by X, giving you a range of [0..X]
- you can then offset that range by adding Y to give you [Y..X+Y]
So in your case, since you want a number between 1 and 20. Y=1, and X=19