generating random tuples in python

Raymond Hettinger python at rcn.com
Wed Apr 22 01:28:59 CEST 2009


[per]
> i realize my example in the original post was misleading. i dont want
> to maximize the difference between individual members of a single
> tuple -- i want to maximize the difference between distinct tuples. in
> other words, it's ok to have (.332, .334, .38), as long as the other
> tuple is, say, (.52, .6, .9) which is very difference from (.332, .
> 334, .38).  i want the member of a given tuple to be arbitrary, e.g.
> something like (rand(), rand(), rand()) but that the tuples be very
> different from each other.

No problem, just define a predicate that formalizes what you mean by
"very different from each other".

def generate_very_different_random_tuples(n):
   selected = set()
   for i in range(n):
        t = rand(), rand(), rand()
        while any(not very_different(t, s) for s in selected):
           t = rand(), rand(), rand()
        selected.add(t)
   return list(selected)


Raymond



More information about the Python-list mailing list