4 ms·
Curious how you would do the lookup if you have an RNG with probabilities 2/7, 2/7, 3/7 (or the same but with significantly larger denominators).
by MauranKilom 6y ago
Curious how you would do the lookup if you have an RNG with probabilities 2/7, 2/7, 3/7 (or the same but with significantly larger denominators).
- tape_measure 6y agoHow about this? In rare circumstances, it may loop forever. def uneven_random(): # return 0 with 2/7 probability, 1 with 2/7 probability, and 2 with 3/7 probability n = random.random() if n < 2.0/7: return 0 if n < 4.0/7: return 1 return 2 def transformed_random(): # return 0 or 1 with 50% probability N = uneven_random() while N == 2 : N = uneven_random() return N