number generator

Dick Moores rdm at rcblue.com
Tue Mar 13 11:12:17 EDT 2007


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,1,1,1] and [1,1,46,1,1], as Python lists, are distinct. I ran
> > this test for M == 8 and N == 4:
>
>Yes, I believe your function is OK. But the 'fencepost' method posted
>earlier in this thread also does this and it's faster AND it's only two
>line of code ...

Yes, I tested that after posting mine. Paul Rubin's fencepost method 
is about 14 times faster than mine for the same M == 8 and N == 4!  :(

> > A = []
> > B = []
> > for x in range(100000):
> >      lst = sumRndInt(8,4)
> >      if lst not in A:
> >          A.append(lst)
> >          B.append(1)
> >      else:
> >          i = A.index(lst)
> >          B[i] += 1
> >
> > A.sort()
>
>Doesn't sorting break the correspondence between A and B?

Yes, but I thought that it would be easier to see that all the 
permutations are represented. It seemed clear enough that with larger 
num, all counts would approach num/35..

>  Also, a more
>pythonic way to count would be to convert the lst into a tuple and then
>do something with a dictionary. Dictionaries have faster lookup. For
>example:
>
>T = tuple(lst)
>D[T] = D.get(T,0) + 1
>
>in the loop in order to count the occurrences.

Sorry, I don't understand this. Could you spell it out for me by 
rewriting my above test to use it? Thanks!

Dick Moores





More information about the Python-list mailing list