Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27950
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Subject | Re: Probability Algorithm |
| Date | 2012-08-26 17:43 -0400 |
| Organization | > Bestiaria Support Staff < |
| References | <CALZWPGh9ng4NoTRB+h0gLpNhEzJoyg+JGXOUjy=TPgqznh7HzQ@mail.gmail.com> <noci38t7cijk2m2624rbggkltm040pef6b@invalid.netcom.com> <CALZWPGgcefcVQQ6-SNuaCX3+g+7H7uJrE4DcScSiWnSSvNNcQg@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3856.1346017426.4697.python-list@python.org> (permalink) |
On Sun, 26 Aug 2012 11:47:57 +0800, ??? <yueyoum@gmail.com> declaimed
the following in gmane.comp.python.general:
> Sorry, missing some conditions
Then, I suggest first produce a proper requirements document stating
ALL conditions, data sources, and results <G>
After all, your final result consists of just three items, drawn
from X lists of unspecified length with Xp probabilities.
Your attempt build subset lists in which the length is equal to the
probability, and THEN draw three items from these sublists is invalid.
Consider where your DLIST starts out with three entries -- but you are
going to reduce it to one entry (probability 1%)...
While a 1% probability is small -- technically it could still be
drawn three times in a row. But since you've already trimmed the DLIST
to one element, and you don't want duplicates in the result, you've
violated your probability requirement. If you'd left DLIST with all
three entries you'd have been able to draw those three -- 1% chance each
time -- and meet the 3-elements, unique, criteria. It may be more
complex if DLIST elements duplicate those in other lists...
Actually, the only limit I see is that the shortest list must
contain at least 3 elements (if only three elements will be in the final
result). That is, in general, each source list must be at least as long
as the result list, as random probabilities could draw each element from
the same list.
When actually drawing from a list, if the element had been drawn
from a /different/ list earlier, just remove it from the current list
and redraw -- don't regenerate the "probability" random number, you've
already determined which list for the probability clause, you just need
to find the unique item in that list, and since each list is at least
the length needed for the result, there must be at least one value that
hasn't been drawn from any other list.
-=-=-=-=-
import random
import copy
RESULT_LEN = 3
PROBABILITIES = [ 43,
37 + 43,
19 + 37 + 43,
1 + 19 + 37 + 43 ]
ALREADY_PICKED_LIST = "a the in on at is not".split()
A_SOURCE = "ask not for whom the bell tolls".split()
B_SOURCE = "it tolls for thee".split()
C_SOURCE = "in the dead of the night the mice will play".split()
D_SOURCE = "thee hast caught me out".split()
def clean(lst, picked=ALREADY_PICKED_LIST):
return [itm for itm in lst if itm not in picked]
def pickOne(lst, picked):
while True:
itm = random.choice(lst)
if itm not in picked: break
lst.remove(itm)
return itm
def generateResult(lsts,
probabilities = PROBABILITIES,
resultLen = RESULT_LEN):
#ensure enough data
if min(lsts, len) < resultLen:
print "Insufficient unique data to ensure desired results"
return None
else:
result = []
for i in range(resultLen):
p = random.randint(0, 99)
if p < probabilities[0]:
lst = lsts[0]
elif p < probabilities[1]:
lst = lsts[1]
elif p < probabilities[2]:
lst = lsts[2]
elif p < probabilities[3]:
lst = lsts[3]
else:
print "Probabilities outside range of 1..100"
break
result.append(pickOne(lst, result))
return result
if __name__ == "__main__":
#build initial lists
sources = [ clean(A_SOURCE),
clean(B_SOURCE),
clean(C_SOURCE),
clean(D_SOURCE) ]
print ""
for i in range(15):
# use copy.deepcopy() so duplicate removal doesn't change master
lists
result = generateResult(copy.deepcopy(sources))
print "Final result: %s" % " | ".join(sorted(result))
-=-=-=-
Final result: dead | it | thee
Final result: for | it | tolls
Final result: bell | thee | whom
Final result: for | thee | tolls
Final result: night | thee | tolls
Final result: ask | for | it
Final result: for | it | night
Final result: for | whom | will
Final result: it | of | whom
Final result: ask | tolls | whom
Final result: ask | for | tolls
Final result: for | play | thee
Final result: dead | thee | whom
Final result: of | tolls | will
Final result: for | of | will
{Actually, you could avoid the deepcopy() above by changing pickOne() to
NOT remove already picked items, but just try rand.choice() until a
new/unique item is picked}
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Probability Algorithm Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-26 17:43 -0400
csiph-web