Madison May added the comment:
[Mark Dickinson]
> Both those seem like clear error conditions to me, though I think it would be
> fine if the second condition produced a ZeroDivisionError rather than a
> ValueError.
Yeah, in hindsight it makes sense that both of those conditions should raise
errors. After all: "Explicit is better than implicit".
As far as optimization goes, could we potentially use functools.lru_cache to
cache the cumulative distribution produced by the weights argument and optimize
repeated sampling?
Without @lru_cache:
>>> timeit.timeit("x = choice(list(range(100)), list(range(100)))", setup="from
>>> random import choice", number=100000)
36.7109281539997
With @lru_cache(max=128):
>>> timeit.timeit("x = choice(list(range(100)), list(range(100)))", setup="from
>>> random import choice", number=100000)
6.6788657720007905
Of course it's a contrived example, but you get the idea.
Walker's aliasing method looks intriguing. I'll have to give it a closer look.
I agree that an efficient implementation would be preferable but would feel out
of place in random because of the return type. I still believe a relatively
inefficient addition to random.choice would be valuable, though.
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue18844>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com