In the initial implementation, the rescale() function was biased and did not evenly sample from all categories (the first and last categories where downweighted). This fixes the issue and adds a test.
rescale()