I need two random numbers. The first one, u, is discrete and takes 70% of the time the value 0 and 30% of the time the value 1. The second one, v, is continuous and takes values uniformly inside [0, 1[.

Can I used only one random number, x, to generate u and v ? Such as:

u = 0 if x < 0.7 else 1
v = x / 0.7 if x < 0.7 else (x - 0.7) / 0.3

I'm wondering if this is correct and do not introduce any bias or artifact (correlation ?) between u and v because they are generated using the same x.

Also (it is more of a computer science question), but what about the floating point imprecisions implied by scaling x to get v ? Finally, is this can be extended if u takes more values (for example, 10% 0, 20% 1 and 70% 2). Thank you.

  • $\begingroup$ Your approach seems to be OK to me. Regarding the floating point imprecision, it is intuitively clear that the "else" part will sample $v$ from more sparsely covered $[0,1[$. $\endgroup$ – Karel Macek Oct 30 '14 at 13:08

Theoretically, your approach is correct. Any correlation introduced between $u$ and $v$ would mean that te original random number $x$ fails some statistical test, i.e. the PRNG used is not very good. However, taking into account that random numbers are generally generated as integers and then sacled to floats, some bias in the lower bits may be introduced (and unavoidably so as you cannot create bits of information out of thin air).

Surely, the method can be extended to more values for $u$, but if any of the $u$-probabilities is too small, there may be too few remaining bits of random for $v$ ...

  • $\begingroup$ Thank you ;) I thought about the lost of precision is case u can take many values, but I did not thought about the simple case where the u-probability can be small, so thank you for pointing this out. $\endgroup$ – Guillaum Oct 30 '14 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.