Re: number generator
- From: *** Moores <rdm@xxxxxxxxxx>
- Date: Sat, 10 Mar 2007 02:32:21 -0800
At 07:17 AM 3/9/2007, cesco wrote:
On Mar 9, 3:51 pm, Paul Rubin <http://phr...@xxxxxxxxxxxxxx> wrote:
> "cesco" <fd.calabr...@xxxxxxxxx> writes:
> > I have to generate a list of N random numbers (integer) whose sum is
> > 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?
>
> Erm, yes, lots of ways, there are probably further constraints on the
> problem, such as the size of the integers, how the lists are supposed
> to be distributed, etc. Can you be more specific? Is this for an
> application? If it's a homework problem, that's fine, but it's better
> in that case for respondents to suggest hints rather than full solutions.
Given two positive integers, N and M with N < M, I have to generate N
positive integers such that sum(N)=M. No more constraints.
So why not just repeatedly call a function to generate lists of length N of random integers within the appropriate range (the closed interval [1,M-N-1]), and return the first list the sum of which is M? I don't understand what all the discussion is about. Time is not one of the constraints.
*** Moores
.
- Follow-Ups:
- Re: number generator
- From: Steven D'Aprano
- Re: number generator
- References:
- number generator
- From: cesco
- Re: number generator
- From: Paul Rubin
- Re: number generator
- From: cesco
- number generator
- Prev by Date: Re: number generator
- Next by Date: Re: unsigned integer?
- Previous by thread: Re: number generator
- Next by thread: Re: number generator
- Index(es):
Loading