dimanche 5 juin 2016

Build a [0, n] random integer generator with a [0, (n+k)] random integer generator

Suppose I have a random integer generator that could generate random integer of [0, 15] and now I want to build one to generate integer within [0, 14] (k=1).

Simply discarding out-of-range number doesn't work in cases when k is large (says [0, 1000] -> [0, 100], k=900) that generator might have to re-generate multiple time and no time is guaranteed to produce a legal output(also a waste of resource and time).

How to I do to make it efficient?




Aucun commentaire:

Enregistrer un commentaire