I'm interested in generating fast random booleans (or equivalently a Bernoulli(0.5) random variable) in C. Of course if one has a fast random generator with a decent statistical behaviour the problem "sample a random Bernoulli(0.5)" is easily solved: sample x
uniformly in (0,1) and return 1
if x<0.5
, 0
otherwise.
Suppose speed is the most important thing, now I have two questions/considerations:
-
Many random doubles generators first generate an integer
m
uniformly in a certain range[0,M]
and then simply return the divisionm/M
. Wouldn't it be faster just to check whetherm < M/2
(hereM/2
is fixed, so we are saving one division)- Is there any faster way to do it? At the end, we're asking for way less statistical properties here: we're maybe still interested in a long period but, for example, we don't care about the uniformity of the distribution (as long as roughly 50% of the values are in the first half of the range).
Aucun commentaire:
Enregistrer un commentaire