This is a problem caused by the fact that you want to create a random number in a smaller range from one in a larger range, where the size of the smaller range is not divided by the size of the larger.
If you had a random number from 0 to 9 (inclusive) and it was necessary to change it to one between 0 and 3, if you just did it trivially, like n% 4, you will have a 3/10 chance of getting a 0 (0, 4 or 8)% 4, but a 2/10 chance to get 3 (3 or 7)% 4. The easiest way here is to simply regenerate a random number if it is greater than 7.
The worst case scenario is when the size of the smaller range is slightly larger than half the size of the larger, so you will have to regenerate a little more than half the time.
Norgg source share