filtering random values (sieves?)
Author Message
filtering random values (sieves?)

I am not sure I am even using the right terminology here, but I
looking for a way to filter random values. Let's say that I am using
random and randrange to generate values from 0-11, but i only want to
accept values 1, 3, 5, 7, 11, (that is just off the top of my head,
could be anything) how would i filter out values that i don't want?
The only way i can think of is just asking the program to choose
again, but that may not be the best way since, at least
hypothetically, you could keep choosing values that are rejected...

Any help would be appreciated.

-kevin

Mon, 02 Aug 2004 00:27:50 GMT
filtering random values (sieves?)
Why don't you put in a list the value you allow to be choosen and make a
random(range(len(list)))?

> I am not sure I am even using the right terminology here, but I
> looking for a way to filter random values. Let's say that I am using
> random and randrange to generate values from 0-11, but i only want to
> accept values 1, 3, 5, 7, 11, (that is just off the top of my head,
> could be anything) how would i filter out values that i don't want?
> The only way i can think of is just asking the program to choose
> again, but that may not be the best way since, at least
> hypothetically, you could keep choosing values that are rejected...
>
> Any help would be appreciated.
>
> -kevin
>

Mon, 02 Aug 2004 00:38:56 GMT
filtering random values (sieves?)
From: "kevin parks"

Quote:
> I am not sure I am even using the right terminology here, but I
> looking for a way to filter random values. Let's say that I am using
> random and randrange to generate values from 0-11, but i only want to
> accept values 1, 3, 5, 7, 11,

Does this help:

Quote:
>>> import random
>>> random.choice([1,3,5,7,11])

7

Emile van Sebille

---------

Mon, 02 Aug 2004 00:48:45 GMT
filtering random values (sieves?)
Uhmm... yeah. stupid me. As amazing as it seems, it never occured to
me to just generate the values i wanted in the first place.

But what i was hoping to do was generate two somewhat large lists
algorhytmically and use one list to filter the other, which would be
sort of  like getting the interesection, like so:

def intersect(*args):
intersection = []
for x in args[0]:
for foo in args[1:]:
if x not in foo: break
else:
intersection.append(x)
return intersection

Not sure that is the best way, but the idea of just choosing from the
items in the first place is, uhmm <embarrasing mode on> obvious
</embarrasing mode off>

It's hard  to find a rock big enough to crawl under. Boy sometimes the

best,

kevin

Mon, 02 Aug 2004 14:54:46 GMT
filtering random values (sieves?)

Quote:

> But what i was hoping to do was generate two somewhat large lists
> algorhytmically and use one list to filter the other, which would be
> sort of  like getting the interesection, ....

You may not need this, but here's an easy way to get the intersection of
two lists... (presuming 2.x)

intersection = [x for x in list1 if x in list2]

If you need to work with 1.5.2 as well, then you could use filter/lambda.
I *think* that it would go like this:

intersection = filter(lambda x: x in list2, list1)

But I'm not certain enough about lambda's syntax to want to promise

Then once you have that, you can easily do the random.choice(intersection)
bit...

Jeff Shannon
Technician/Programmer
Credit International

Tue, 03 Aug 2004 02:53:41 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages