number generator
Hendrik van Rooyen
mail at microcorp.co.za
Tue Mar 13 07:20:49 CET 2007
"Nick Craig-Wood" <nick at craig-wood.com> 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
More information about the Python-list
mailing list