Re: number generator

2007-03-14 Thread Anton Vredegoor
Anton Vredegoor wrote: > def memoize(fn): > cache = {} > def proxy(*args): > try: return cache[args] > except KeyError: return cache.setdefault(args, fn(*args)) > return proxy Sorry this doesn't work in this case. This works: def memoize(fn): cache = {

Re: number generator

2007-03-14 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > >>> list(_partitions(25, 24)) > [(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2)] > >>> list(_partitions(25, 25)) > [] Hmm. I'll look into this later if I get a chance. Thanks. -- http://mail.python.org/mailman/listinfo/pyth

Re: number generator

2007-03-14 Thread Anton Vredegoor
Paul Rubin wrote: >> def genpool(n, m): >> if n == 1: >> yield [m] >> else: >> for i in xrange(1, m): >> for rest in genpool(n-1, m-i): >> yield rest + [i] >> >> import random >> print random.choice(list(genpool(n=4, m=20))) > > This generates a

Re: number generator

2007-03-14 Thread Paul Rubin
"Raymond Hettinger" <[EMAIL PROTECTED]> writes: > Since people are posting their solutions now (originally only hints > were provided for the homework problem), here's mine: > > def genpool(n, m): > if n == 1: > yield [m] > else: > for i in xrange(1, m): > for r

Re: number generator

2007-03-14 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > If you generate all the possible sets of five numbers between 1 and 50, > there are 50**5 of them, not 2611. Yes, the idea is generate only the sets that add up to 50. > unique_partitions(60, 6) takes 0.8 seconds; unique_partitions(80, 8) > takes abou

Re: number generator

2007-03-14 Thread Shane Geiger
Raymond: It looks to me as if you are trying to turn a generator into a list in the final line. That doesn't work. Raymond Hettinger wrote: To make the solutions equi-probable, a simple approach is to recursively enumerate all possibilities and then choose one of them with random.choice().

Re: number generator

2007-03-14 Thread Duncan Smith
Hendrik van Rooyen wrote: > "Duncan Smith" <[EMAIL PROTECTED]> wrote: > > > >>Yes, if the generating processes yield numbers from different >>probability mass functions. You could simply look at the likelihood >>ratio. Otherwise, the likelihood ratio will be 1. >> > > I was thinking about th

Re: number generator

2007-03-14 Thread Alex Martelli
Raymond Hettinger <[EMAIL PROTECTED]> wrote: > [Alex Martelli] > > map([random.randrange(5) for i in xrange(45)].count, xrange(5)) > > > > i.e., this gives 5 integers (each between 0 and 45 included) summing to > > 45 -- add 1 to each of them to get the desired result. > > This is a really nice

Re: number generator

2007-03-14 Thread Duncan Smith
Duncan Smith wrote: > greg wrote: > >>Gabriel Genellina wrote: >> >> >>>The 5th number is not "random". >> >> >>More precisely, the fifth number is not *independent* >>of the others. You can't have five independent random >>numbers that sum to 50; only four independent numbers >>plus a dependent o

Re: number generator

2007-03-14 Thread cesco
> Since people are posting their solutions now (originally only hints > were provided for the homework problem), here's mine: Well, I never really said it was a homework problem. Sure there are more implications to my question that I had first imagined and I'm really happy I could get so many insi

Re: number generator

2007-03-14 Thread Anton Vredegoor
Raymond Hettinger wrote: > Since people are posting their solutions now (originally only hints > were provided for the homework problem), here's mine: Homework problem? Do you have some information from the OP that I can't find in this thread? Anyway, I consider the 'homework' idea and the asso

Re: number generator

2007-03-14 Thread Raymond Hettinger
[Alex Martelli] > map([random.randrange(5) for i in xrange(45)].count, xrange(5)) > > i.e., this gives 5 integers (each between 0 and 45 included) summing to > 45 -- add 1 to each of them to get the desired result. This is a really nice approach. Besides being fast, it is not too hard to see th

Re: number generator

2007-03-14 Thread Hendrik van Rooyen
"Steven D'Aprano" <[EMAIL PROTECTED]> wrote: > > Consider the distance between each paid of consecutive poles. The sum of > the distances must add up to the distance from the first to the last, and > if there are two fixed poles plus five in between, there are five > distances. No there are six

Re: number generator

2007-03-14 Thread Raymond Hettinger
> To make the solutions equi-probable, a simple approach is to > recursively enumerate all possibilities and then choose one of them > with random.choice(). Since people are posting their solutions now (originally only hints were provided for the homework problem), here's mine: def genpool(n, m):

Re: number generator

2007-03-14 Thread Steven D'Aprano
On Wed, 14 Mar 2007 19:25:46 +1100, Steven D'Aprano wrote: > Now that I've seen your _partition() function, I'm quite impressed. It is > still brute-force, but it avoids generating all 50**5 non-unique lists. By > my count, it only has to throw away 11,294 lists to generate the 2611 > unique lists

Re: number generator

2007-03-14 Thread Steven D'Aprano
On Tue, 13 Mar 2007 23:08:38 -0800, Paul Rubin wrote: > Steven D'Aprano <[EMAIL PROTECTED]> writes: >> > It should be possible to enumerate all sets of five numbers which sum >> > to 50 and randomly select one of them. >> >> Of course it's possible. It's also a very inefficient way of doing so. F

Re: number generator

2007-03-14 Thread Hendrik van Rooyen
"Gabriel Genellina" <[EMAIL PROTECTED]> wrote: >You can't have two different sets with four equal numbers - it's not a >very difficult thing, it's impossible to distinguish because they're >identical! >Given 4 numbers in the set, the 5th is uniquely determined. By example: >12, 3, 10, 18 *must* e

Re: number generator

2007-03-14 Thread Hendrik van Rooyen
"Steven D'Aprano" <[EMAIL PROTECTED]> wrote: > Easy-peasey. Just collate the individual numbers from the lists, and graph > their frequencies. You will get quite different distributions. Both are > "random", but in different ways. > 8< code - Thanks - I think too com

Re: number generator

2007-03-14 Thread Hendrik van Rooyen
"Duncan Smith" <[EMAIL PROTECTED]> wrote: > Yes, if the generating processes yield numbers from different > probability mass functions. You could simply look at the likelihood > ratio. Otherwise, the likelihood ratio will be 1. > I was thinking about the same random number generator being use

Re: number generator

2007-03-13 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > > It should be possible to enumerate all sets of five numbers which sum > > to 50 and randomly select one of them. > > Of course it's possible. It's also a very inefficient way of doing so. For > five numbers between 1 and 50, there are 50**5 = 312,500

Re: number generator

2007-03-13 Thread Steven D'Aprano
On Tue, 13 Mar 2007 20:42:17 -0700, [EMAIL PROTECTED] wrote: > It should be possible to enumerate all sets of five numbers which sum > to 50 and randomly select one of them. Of course it's possible. It's also a very inefficient way of doing so. For five numbers between 1 and 50, there are 50**5 =

Re: number generator

2007-03-13 Thread Paul Rubin
Paul Rubin writes: > # yield all partitions of n having length k, with many duplications > def _partitions(n,k): > if k==0: return > for i in xrange(1,n-k+1): > for p in partitions(n-i, k-1): > yield (i,)+p Bah, I manag

Re: number generator

2007-03-13 Thread [EMAIL PROTECTED]
It should be possible to enumerate all sets of five numbers which sum to 50 and randomly select one of them. Cheers, -T -- http://mail.python.org/mailman/listinfo/python-list

Re: number generator

2007-03-13 Thread Dick Moores
At 06:20 PM 3/13/2007, Paul Rubin wrote: >Dick Moores <[EMAIL PROTECTED]> writes: > > I understand what zip() and random.sample() are doing, and the above > > helps me get inside the fencepost method, but I don't understand WHY > > it works, or how in the world anyone could have devised it. It is >

Re: number generator

2007-03-13 Thread Duncan Smith
greg wrote: > Gabriel Genellina wrote: > >> The 5th number is not "random". > > > More precisely, the fifth number is not *independent* > of the others. You can't have five independent random > numbers that sum to 50; only four independent numbers > plus a dependent one. > > -- > Greg In the

Re: number generator

2007-03-13 Thread Duncan Smith
Hendrik van Rooyen wrote: > "Nick Craig-Wood" <[EMAIL PROTECTED]> wrote: > > >>Paul Rubin 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 anothe

Re: number generator

2007-03-13 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > Me too. Although Alex Martelli's algorithm: > > map([random.randrange(5) for i in xrange(45)].count, xrange(5)) > > (each value needs adjusting up by one) really boggles my brain. I'm going > to have to think about that. Heh, that is woefully ineffic

Re: number generator

2007-03-13 Thread Steven D'Aprano
On Tue, 13 Mar 2007 08:20:49 +0200, Hendrik van Rooyen wrote: > 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

Re: number generator

2007-03-13 Thread Paul Rubin
Dick Moores <[EMAIL PROTECTED]> writes: > I understand what zip() and random.sample() are doing, and the above > helps me get inside the fencepost method, but I don't understand WHY > it works, or how in the world anyone could have devised it. It is > truly magical to me! It's Gerald Flanagan's te

Re: number generator

2007-03-13 Thread Steven D'Aprano
On Tue, 13 Mar 2007 17:53:41 -0700, Dick Moores wrote: > At 05:47 PM 3/10/2007, Paul Rubin 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])] > > = > M

Re: number generator

2007-03-13 Thread Dick Moores
At 05:47 PM 3/10/2007, Paul Rubin 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])] = M = 50 N = 4 def sumRndIntRubin(M, N): import random t = sort

Re: number generator

2007-03-13 Thread greg
Gabriel Genellina wrote: > The 5th number is not "random". More precisely, the fifth number is not *independent* of the others. You can't have five independent random numbers that sum to 50; only four independent numbers plus a dependent one. -- Greg -- http://mail.python.org/mailman/listinfo/py

Re: number generator

2007-03-13 Thread Gabriel Genellina
En Tue, 13 Mar 2007 03:20:49 -0300, Hendrik van Rooyen <[EMAIL PROTECTED]> escribió: > 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 a

Re: number generator

2007-03-13 Thread Paul Rubin
"Terry Reedy" <[EMAIL PROTECTED]> writes: > P(m,n) has no known exact formula but can be computed > inductively rather easily. The partitions for m and n can be ranked in > lexicographic order from 0 to P(m,n)-1. Given a rank r in that range, one > can calculate the particular partition that

Re: number generator

2007-03-13 Thread Anton Vredegoor
Dick Moores wrote: > Paul Rubin's fencepost method is about 14 times faster than mine for > the same M == 8 and N == 4! :( Actually they looked a bit similar after I had mucked a bit with them :-) But indeed it's slow. > Sorry, I don't understand this. Could you spell it out for me by > rewri

Re: number generator

2007-03-13 Thread Dick Moores
At 06:59 AM 3/13/2007, Anton Vredegoor wrote: >Dick Moores wrote: > > > If the added constraint is instead that the probability of generating > > a given list of length N be the same as that of generating any other > > list of length N, then I believe my function does the job. Of course, > > [1,46,

Re: number generator

2007-03-13 Thread Duncan Booth
Dick Moores <[EMAIL PROTECTED]> wrote: >>On the other hand, making sure that each combination occurs equally >>often (as your example might imply) is doable but still leaves the >>question whether the order of the numbers matters: are [1,46,1,1,1] >>and [1,1,46,1,1] the same or different combinati

Re: number generator

2007-03-13 Thread Anton Vredegoor
Dick Moores wrote: > If the added constraint is instead that the probability of generating > a given list of length N be the same as that of generating any other > list of length N, then I believe my function does the job. Of course, > [1,46,1,1,1] and [1,1,46,1,1], as Python lists, are distinc

Re: number generator

2007-03-13 Thread Dick Moores
At 02:52 AM 3/13/2007, Duncan Booth wrote: >Dick Moores <[EMAIL PROTECTED]> wrote: > > > But let's say there is one more constraint--that for each n of the N > > positive integers, there must be an equal chance for n to be any of > > the integers between 1 and M-N+1, inclusive. Thus for M == 50 and

Re: number generator

2007-03-13 Thread Duncan Booth
Dick Moores <[EMAIL PROTECTED]> wrote: > But let's say there is one more constraint--that for each n of the N > positive integers, there must be an equal chance for n to be any of > the integers between 1 and M-N+1, inclusive. Thus for M == 50 and N >== 5, the generated list of 5 should be as l

Re: number generator

2007-03-12 Thread Dick Moores
At 06:38 AM 3/10/2007, Steven D'Aprano wrote: >On Sat, 10 Mar 2007 02:32:21 -0800, Dick Moores wrote: > > > 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

Re: number generator

2007-03-12 Thread Hendrik van Rooyen
"Nick Craig-Wood" <[EMAIL PROTECTED]> wrote: > Paul Rubin 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 reas

Re: number generator

2007-03-12 Thread Carsten Haese
On Sat, 2007-03-10 at 22:27 -0500, Terry Reedy wrote: > "Anton Vredegoor" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > | Terry Reedy wrote: > | > | > Partitioning positive count m into n positive counts that sum to m is a > | > standard combinatorial problem at least 300 years o

Re: number generator

2007-03-12 Thread Nick Craig-Wood
Paul Rubin 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 rea

Re: number generator

2007-03-12 Thread Duncan Booth
[EMAIL PROTECTED] (Alex Martelli) wrote: > Without any specification regarding the distributions required for the > "5 random numbers" it's really impossible to say whether these are > better or worse than other proposed solutions. FWIW, I decided it would be fun to see what kind of implementatio

Re: number generator

2007-03-11 Thread Alex Martelli
Army1987 <[EMAIL PROTECTED]> wrote: > "cesco" <[EMAIL PROTECTED]> ha scritto nel messaggio > news:[EMAIL PROTECTED] > >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

Re: number generator

2007-03-11 Thread Army1987
"cesco" <[EMAIL PROTECTED]> ha scritto nel messaggio news:[EMAIL PROTECTED] >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 > simpl

Re: number generator

2007-03-11 Thread MonkeeSage
On Mar 11, 2:16 am, greg <[EMAIL PROTECTED]> wrote: > MonkeeSage wrote: > > this ... requires that M be evenly divisible by N, > > No, it doesn't -- I never said the numbers had > to be *equal*. Sorry for not being clear. I was refering to my specific implementation of the algorithm, not the gener

Re: number generator

2007-03-10 Thread greg
MonkeeSage wrote: > this ... requires that M be evenly divisible by N, No, it doesn't -- I never said the numbers had to be *equal*. > and only works well with smaller N values, Why? > and selections are limited to numbers in the > 1 to (M/N)+(M/N) range I don't understand what you mean by t

Re: number generator

2007-03-10 Thread MonkeeSage
On Mar 10, 11:26 pm, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > To compare to the "cheat" method, calculate the mean and standard > deviation of this sample, and compare to those from the other method. I belieive you (mainly because I'm too lazy to write the sieve, hehe). ;) Regards, Jordan --

Re: number generator

2007-03-10 Thread Steven D'Aprano
On Sat, 10 Mar 2007 17:19:38 -0800, MonkeeSage wrote: > On Mar 10, 6:47 pm, Paul Rubin 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])] > > Simpler, true, b

Re: number generator

2007-03-10 Thread Steven D'Aprano
On Sat, 10 Mar 2007 16:02:56 -0800, Paul Rubin wrote: > Steven D'Aprano <[EMAIL PROTECTED]> writes: >> > By your method, what is the probability of the first number being >> > higher than 30? What is the probability of the fifth number being >> > higher than 30? If these probabilities are unequa

Re: number generator

2007-03-10 Thread shellux
On Mar 9, 10:44 pm, "cesco" <[EMAIL PROTECTED]> wrote: > 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

Re: number generator

2007-03-10 Thread Anton Vredegoor
Terry Reedy wrote: > "Anton Vredegoor" <[EMAIL PROTECTED]> wrote in message > | Yes that was one of my first ideas too. But later on Steven pointed out > | that one can view the problem like this: > | > | 0001100010100 > | > | That would be [3,4,3,1,2] > | > | where the '1' elements are like

Re: number generator

2007-03-10 Thread Terry Reedy
"Anton Vredegoor" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | Terry Reedy wrote: | | > Partitioning positive count m into n positive counts that sum to m is a | > standard combinatorial problem at least 300 years old. The number of such | > partitions, P(m,n) has no known exa

Re: number generator

2007-03-10 Thread Paulo da Silva
Paul Rubin escreveu: > Paulo da Silva <[EMAIL PROTECTED]> writes: >> May be this is what you want ... >> I didn't test it enough ... but seems fine. > > That's way too complicated. Think about Gerald Flanagan's description > of the telegraph poles, and how to implement simply. It is a two liner.

Re: number generator

2007-03-10 Thread MonkeeSage
On Mar 10, 6:47 pm, Paul Rubin 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])] Simpler, true, but I don't think it gives any better distribution... import rand

Re: number generator

2007-03-10 Thread Paul Rubin
"MonkeeSage" <[EMAIL PROTECTED]> writes: > Taking your comment and running with it...this is pretty much > cheating, and requires that M be evenly divisible by N, and only works > well with smaller N values, and selections are limited to numbers in > the 1 to (M/N)+(M/N) range...but hey; other than

Re: number generator

2007-03-10 Thread MonkeeSage
On Mar 10, 3:16 am, greg <[EMAIL PROTECTED]> wrote: > Another possibility is to generate a list of N non-random > numbers that sum to M, and then adjust them up or down > by random amounts. By performing up/down adjustments in > pairs, you can maintain the sum invariant at each step. > So then it's

Re: number generator

2007-03-10 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > > By your method, what is the probability of the first number being > > higher than 30? What is the probability of the fifth number being > > higher than 30? If these probabilities are unequal, can we really say > > the sequences are random? > > Of c

Re: number generator

2007-03-10 Thread Anton Vredegoor
Anton Vredegoor wrote: > L = [1] * (bins-1) + [0] * (bins-1) replace these lines in the code by: L = [1] * (bins-1) + [0] * (bricks-bins) A. -- http://mail.python.org/mailman/listinfo/python-list

Re: number generator

2007-03-10 Thread Steven D'Aprano
On Sat, 10 Mar 2007 08:29:09 -0800, Paul Rubin wrote: > Steven D'Aprano <[EMAIL PROTECTED]> writes: >> 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. > > What does it mean for the first

Re: number generator

2007-03-10 Thread Anton Vredegoor
Terry Reedy wrote: > Partitioning positive count m into n positive counts that sum to m is a > standard combinatorial problem at least 300 years old. The number of such > partitions, P(m,n) has no known exact formula but can be computed > inductively rather easily. The partitions for m and n

Re: number generator

2007-03-10 Thread Terry Reedy
"Anton Vredegoor" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | Raymond Hettinger wrote: | | > To make the solutions equi-probable, a simple approach is to | > recursively enumerate all possibilities and then choose one of them | > with random.choice(). | | Maybe it is possible to

Re: number generator

2007-03-10 Thread Paul Rubin
Paulo da Silva <[EMAIL PROTECTED]> writes: > May be this is what you want ... > I didn't test it enough ... but seems fine. That's way too complicated. Think about Gerald Flanagan's description of the telegraph poles, and how to implement simply. It is a two liner. -- http://mail.python.org/mai

Re: number generator

2007-03-10 Thread Paulo da Silva
cesco escreveu: > 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? > > Than

Re: number generator

2007-03-10 Thread Mel Wilson
Gerard Flanagan wrote: > On Mar 9, 4:17 pm, "cesco" <[EMAIL PROTECTED]> wrote: >> On Mar 9, 3:51 pm, Paul Rubin wrote: >> >>> "cesco" <[EMAIL PROTECTED]> writes: I have to generate a list of N random numbers (integer) whose sum is equal to M. If, for example, I

Re: number generator

2007-03-10 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > 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. What does it mean for the first 4 numbers to be random? For example, is 27 random? By your met

Re: number generator

2007-03-10 Thread Anton Vredegoor
Raymond Hettinger wrote: > To make the solutions equi-probable, a simple approach is to > recursively enumerate all possibilities and then choose one of them > with random.choice(). Maybe it is possible to generate the possibilities by an indexing function and then use randint to pick one of the

Re: number generator

2007-03-10 Thread Steven D'Aprano
On Fri, 09 Mar 2007 18:41:39 +, Dennis Lee Bieber wrote: > On 9 Mar 2007 06:44:01 -0800, "cesco" <[EMAIL PROTECTED]> declaimed > the following in comp.lang.python: > >> 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 ra

Re: number generator

2007-03-10 Thread Steven D'Aprano
On Sat, 10 Mar 2007 02:32:21 -0800, Dick Moores wrote: > 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 t

Re: number generator

2007-03-10 Thread Dick Moores
At 07:17 AM 3/9/2007, cesco wrote: >On Mar 9, 3:51 pm, Paul Rubin wrote: > > "cesco" <[EMAIL PROTECTED]> 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 > > > s

Re: number generator

2007-03-10 Thread Klaus Alexander Seistrup
Raymond Hettinger wrote: > To make the solutions equi-probable, a simple approach is to > recursively enumerate all possibilities and then choose one > of them with random.choice(). Or create a list using the biased method, then use .shuffle() to return another permutation. Cheers, -- Klaus

Re: number generator

2007-03-10 Thread greg
Steven D'Aprano wrote: > Last but not least, another possible algorithm is to start with a list of > N numbers, regardless of whether or not they add to M, and then adjust > each one up or down by some amount until they sum to the correct value. Another possibility is to generate a list of N non-

Re: number generator

2007-03-10 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Raymond Hettinger wrote: > On Mar 9, 7:32 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: >> In <[EMAIL PROTECTED]>, cesco wrote: >> > Given two positive integers, N and M with N < M, I have to generate N >> > positive integers such that sum(N)=M. No more constraint

Re: number generator

2007-03-10 Thread Raymond Hettinger
On Mar 9, 7:32 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > In <[EMAIL PROTECTED]>, cesco wrote: > > 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. > > Break it into subproblems. Generate a random nu

Re: number generator

2007-03-09 Thread Steven D'Aprano
On Fri, 09 Mar 2007 06:44:01 -0800, cesco wrote: > 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 Py

Re: number generator

2007-03-09 Thread Bjoern Schliessmann
cesco wrote: > On Mar 9, 3:51 pm, Paul Rubin >> "cesco" <[EMAIL PROTECTED]> 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

Re: number generator

2007-03-09 Thread Paul Rubin
Carsten Haese <[EMAIL PROTECTED]> writes: > > 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. > Paul still had a point, since the word "positive" is a vital piece of > information that never appeared in your origin

Re: number generator

2007-03-09 Thread Carsten Haese
On Fri, 2007-03-09 at 07:17 -0800, cesco wrote: > On Mar 9, 3:51 pm, Paul Rubin wrote: > > "cesco" <[EMAIL PROTECTED]> 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

Re: number generator

2007-03-09 Thread Gerard Flanagan
On Mar 9, 4:17 pm, "cesco" <[EMAIL PROTECTED]> wrote: > On Mar 9, 3:51 pm, Paul Rubin wrote: > > > "cesco" <[EMAIL PROTECTED]> 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

Re: number generator

2007-03-09 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, cesco wrote: > 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. Break it into subproblems. Generate a random number X from a suitable range and you are left with one number, and the problem

Re: number generator

2007-03-09 Thread cesco
On Mar 9, 3:51 pm, Paul Rubin wrote: > "cesco" <[EMAIL PROTECTED]> 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

Re: number generator

2007-03-09 Thread Paul Rubin
"cesco" <[EMAIL PROTECTED]> 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 accomp