I have been trying to understand how using modulus can be dangerous and can introduce nasty but into the software.
Here is an illustration:
https://gist.github.com/anon7238593-create/5719c2ac8824650abbac4592ceee9408
in the notebook we can see that we used modulus to generate another random variable range which seems correct. you try to generate few number and they looks random but are they? no. here for sake of simplicity I have choose random range of 0-255 which I then convert to 0-100 range. here the probability that 0 comes is greater than having 100 because 101 doesn't evenly divide 255. there is a remainder of 53. which means the first 53 numbers are more likely to be chosen than the rest of the numbers which we can see in the graph.
you might be wondering why? it's rather simple. there are exactly 3 numbers in 0-255 that maps to number let's say 3 ( or any number less than or equal to 53 ). while there are only 2 numbers that maps to any number greater than 53. this affects their likeliness of being chosen.
which case this is fine?
only and only when the number of elements in bigger range is evenly divisible by the number of elements in smaller range.
eg, ~~0-2 ( 3 elements ) 255 % 3 = 0~~ 0-4 ( 4 elements ) 256 % 4 = 0
in this case the likeliness of an element being chosen doesn't change.
hope this was useful :)
Edit:
off by one correction. thanks to @eleijeep@piefed.social
If you use something like four bytes instead of one, your error drops dramatically... If you're generating numbers from 0-100, You'd have ~50 "bad" cases (just imagine the remainder is the same) out of 4 billion instead of ~50 out of 256. Unless you're running a casino or something (and even then...) it's probably "fine enough" but of course you have to know about it!
that's a great observation. the difference in size of the original evenly distributed random integer space and smaller integer space is important too. if the difference is big enough the smaller random integer space will be equally likely.