문제

The usual method to generate a uniform random number 0..n using coin flips is to build a rng for the smallest power of two greater than n in the obvious way, then whenever this algorithm generates a number larger than n-1, throw that number away and try again.

Unfortunately this has worst case runtime of infinity.

Is there any way to solve this problem while guaranteeing termination?

도움이 되었습니까?

해결책

Quote from this answer https://stackoverflow.com/a/137809/261217:

There is no (exactly correct) solution which will run in a constant amount of time, since 1/7 is an infinite decimal in base 5.

Now ask Adam Rosenfield why it is true :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top