number generator
Duncan Booth
duncan.booth at invalid.invalid
Tue Mar 13 05:52:06 EDT 2007
Dick Moores <rdm at rcblue.com> 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 likely to be [1,46,1,1,1]
> as [10,10,10,10,10] or [14, 2, 7, 1, 26].
I don't think what you wrote actually works. Any combination including a 46
must also have four 1s, so the digit 1 has to be at least 4 times as likely
to appear as the digit 46, and probably a lot more likely than that.
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 combinations?
The sorted/sample/xrange solution seems to give an even distribution or
ordered combinations, I don't know what algorithm (short of enumerating all
possible solutions and choosing one at random) gives an even distribution
when the ordering doesn't matter.
More information about the Python-list
mailing list