Re: number generator
- From: "Hendrik van Rooyen" <mail@xxxxxxxxxxxxxxx>
- Date: Tue, 13 Mar 2007 08:20:49 +0200
"Nick Craig-Wood" <nick@xxxxxxxxxxxxxx> wrote:
Paul Rubin <http> wrote:
The fencepost method still seems to be simplest:
t = sorted(random.sample(xrange(1,50), 4))
print [(j-i) for i,j in zip([0]+t, t+[50])]
Mmm, nice.
Here is another effort which is easier to reason about the
distribution produced but not as efficient.
def real(N, M):
while 1:
t = [ random.random() for i in range(N) ]
factor = M / sum(t)
t = [ int(round(x * factor)) for x in t]
if sum(t) == M:
break
print "again"
assert len(t) == N
assert sum(t) == M
return t
It goes round the while loop on average 0.5 times.
If 0 isn't required then just test for it and go around the loop again
if found. That of course skews the distribution in difficult to
calculate ways!
I have been wondering about the following as this thread unrolled:
Is it possible to devise a test that can distinguish between sets
of:
- five random numbers that add to 50, and
- four random numbers and a fudge number that add to 50?
My stats are way too small and rusty to attempt to answer
the question, but it seems intuitively a very difficult thing.
- Hendrik
.
- Follow-Ups:
- Re: number generator
- From: Steven D'Aprano
- Re: number generator
- From: Duncan Smith
- Re: number generator
- References:
- number generator
- From: cesco
- Re: number generator
- From: Steven D'Aprano
- Re: number generator
- From: greg
- Re: number generator
- From: MonkeeSage
- Re: number generator
- From: Paul Rubin
- Re: number generator
- From: Nick Craig-Wood
- number generator
- Prev by Date: Re: Program __main__ function
- Next by Date: Calling cpp from python/SWIG
- Previous by thread: Re: number generator
- Next by thread: Re: number generator
- Index(es):
Relevant Pages
|