Re: math problem from an interview
- From: Richard Heathfield <invalid@xxxxxxxxxxxxxxx>
- Date: Mon, 11 Sep 2006 07:15:45 +0000
Pascal Bourguignon said:
"Digital Puer" <digital_puer@xxxxxxxxxxx> writes:
Suppose you have a function that returns random 64-bit integers
without checking if a particular integer has already been used. How
many integers would be returned before the probability that an integer
has been repeated is above 0.5?
Depends on how 'random' it is.
If the function is _equiprobable_ on the whole range 0 2^64-1, then
you'll have to fetch 2^63 integers before the probability of getting a
repeat be above 0.5.
You reckon?
If you have n items, and choose m of them at random (equiprobably, as you
suggest), then the probability of two of them *not* being the same is:
n! / (n - m)!
-------------
m
n
For 1 bit, if you fetch two numbers, the probability of a clash is 0.5. If
you fetch a third, the probability is 1.
For 2 bits, you have to fetch three numbers to exceed 0.5.
Bits Number reqd Number of bits
to exceed in result
0.5 collision
probability
1 3 2
2 3 2
3 4 2
4 5 3
5 7 3
6 10 4
7 14 4
8 20 5
9 27 5
10 38 6
11 54 6
12 76 7
13 106 7
14 151 8
15 214 8
16 302 9
32 77163 17
As you can see, the number of bits required to represent the number you have
to fetch is approximately half the number of bits in the range, and this
approximation appears to hold good as the numbers get bigger.
So I'd expect the solution to be in the range 2^32 - 2^34 (ish), which is a
far cry from 2^63.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
.
- Follow-Ups:
- Re: math problem from an interview
- From: Pascal Bourguignon
- Re: math problem from an interview
- From: Googmeister
- Re: math problem from an interview
- References:
- math problem from an interview
- From: Digital Puer
- Re: math problem from an interview
- From: Pascal Bourguignon
- math problem from an interview
- Prev by Date: Re: math problem from an interview
- Next by Date: Re: math problem from an interview
- Previous by thread: Re: math problem from an interview
- Next by thread: Re: math problem from an interview
- Index(es):
Relevant Pages
|
Loading