Biased random?

mensanator at aol.com mensanator at aol.com
Mon Aug 27 18:30:16 EDT 2007


On Aug 27, 3:42 pm, Ivan Voras <ivoras at __fer.hr__> wrote:
> Hi,
>
> I have a list of items, and need to choose several elements from it,
> "almost random". The catch is that the elements from the beginning
> should have more chance of being selected than those at the end (how
> much more? I don't care how the "envelope" of probability looks like at
> this point - can be linear). I see that there are several functions in
> Python standard libraries for various distribution, but is there an easy
> pythonic way to make them do what I need?
>
>  signature.asc
> 1KDownload

If your list is small enough, you could do something
like this:

import random
test = ['a','b','c','d','e','f','g','h','i','j']
temp = []
size = len(test)-1
for i,j in enumerate(test):
  temp.extend(j*(2**(size-i)))
for i in xrange(400):
  if i % 30 == 0: print
  print random.choice(temp),

##  a a b a a a i c a a a b c b b a a a a b a a a d a b a a g f
##  a e a a a i a b a a b c b a a b a b b c b a b a c a a a a a
##  b a a c a c e d b d a a a a a c a b b e a a b a a b a a c a
##  a a a a a a b a b a c a a a c a d a c a a d b b b b d a a a
##  a a d b a a a b a b b a b a c a a a b b a c b c a a c c c a
##  a a b a a a b a b a a a a d c a a b c b b b d b b a a b c a
##  a a a a a a b a d b c d b a b c a d b b b a b a b b b b b a
##  b c a b a a b c a a a a a a a a b a a a b a a a a a d a b b
##  a b b a c a b c a a a a a a a a c b a c c a a e a a a c b b
##  a c e b b a a b c a b a b a a b a g a b e a a a c c a a c b
##  b i a b a a a a c c c b a a a a a b b b a b b a a b b h a a
##  d b a b a b b a a b c a b a a b a a a c a d a a b a b a a a
##  a d a b b a b a a c a b c d b a a d a a b b a a a a a d a c
##  a a a b a a b a c b

Here, 'a' is twice as likely to occur as 'b',
which is twice as likely to occur as 'c',
which is twice as likely to occur as 'd',
etc.




More information about the Python-list mailing list