The way you've written it above is actually good idiomatic python. If we analyze the algorithm we'll find it's essentially doing this:
- Making a list of elements that satisfy the predicate. (Grows linearly with n)
- Choosing a random element from that list. (Constant time)
The only other way to go about it would be to choose an element at random, decide if it satisfies the predicate, and choose again if it does not. This algorithm is a little more complex. In the case where 90% of the list satisfies the predicate, this will run much faster than your solution. In the case where only 10% of the list satisfies the predicate, it will actually run much slower, because there's a good chance it will randomly select a given element and check if the predicate is satisfied on that element more than once. Now you could consider memoizing your predicate, but you're still going to select a lot of random data. It comes down to this: Unless your solution is particularly unsuited for your data, stick with it, because it's great. Personally, I'd rewrite it like this:
intlist = range(1,10)
randomeven = random.choice([i for i in intlist if i % 2 == 0])
This is a little more concise, but it's going to run exactly the same as your existing code.