Re: number generator
- From: Steven D'Aprano <steve@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 11 Mar 2007 00:45:30 +1100
On Fri, 09 Mar 2007 18:41:39 +0000, Dennis Lee Bieber wrote:
On 9 Mar 2007 06:44:01 -0800, "cesco" <fd.calabrese@xxxxxxxxx> declaimed
the following in comp.lang.python:
I have to generate a list of N random numbers (integer) whose sum isWell... Just that the last number is not random -- it has to equal
equal to M. If, for example, I have to generate 5 random numbers whose
sum is 50 a possible solution could be [3, 11, 7, 22, 7]. Is there a
simple pattern or function in Python to accomplish that?
(using your limit of 50 and 5 numbers):
fifth = 50 - sum(first, second, third, fourth)
Doesn't mean that it isn't random. After all, the first four numbers are
random, therefore their sum is random. 50 - (something random) is also
random.
In practice, one would deterministically generate the last number needed,
but that last number is unpredictable because the first four numbers are
unpredictable.
Remember that random numbers are not necessarily unconstrained, nor are
they necessarily uniformly distributed. We blithely talk about "random
numbers", but of course there is a constraint that we're selecting from a
finite range. Any finite range of numbers, no matter how big, is a
vanishingly small percentage of all the possible numbers.
--
Steven.
.
- Follow-Ups:
- Re: number generator
- From: Paul Rubin
- Re: number generator
- References:
- number generator
- From: cesco
- number generator
- Prev by Date: Re: number generator
- Next by Date: UTF-8
- Previous by thread: Re: number generator
- Next by thread: Re: number generator
- Index(es):
Relevant Pages
|