# [Python-ideas] random.choice on non-sequence

Terry Reedy tjreedy at udel.edu
Wed Apr 13 19:29:53 EDT 2016

```On 4/13/2016 6:46 AM, Chris Angelico wrote:
> On Wed, Apr 13, 2016 at 8:36 PM, Terry Reedy <tjreedy at udel.edu> wrote:
>> On 4/13/2016 12:52 AM, Chris Barker - NOAA Federal wrote:
>>
>>> BTW, isn't it impossible to randomly select from an infinite iterable
>>> anyway?
>>
>>
>> With equal probability, yes, impossible.

I have seen too many mathematical or statistical writers who ought to
know better write "Let N be a random integer..." with no indication of a
finite bound or other than a uniform distribution.  No wonder students

>> With skewed probabilities, no, possible.
>
> What, you mean like this?
>
> def choice(it):
>      it = iter(it)
>      value = next(it)
>      try:
>          while random.randrange(2):
>              value = next(it)
>      except StopIteration: pass
>      return value

With a perfect source of random numbers, this will halt with probability
one.  With current pseudorandom generators, this will definitely halt.
And yes, both theoretically and practically, this is an example of
skewed probabilities  -- a waiting time distribution.

> I'm not sure how useful it is, but it does accept potentially infinite
> iterables, and does return values selected at random...

People often want variates selected from a non-uniform distribution.
Often, a uniform variate can be transformed.  Sometimes multiple uniform
variates are needed.

If 'it' above is itertools.count, the above models the waiting time to
get 'heads' (or 'tails') with a fair coin.  Waiting times might be
obtained more efficiently, perhaps, with, say, randrange(2**16), with
another draw for as long as the value is 0 plus some arithmethic that
uses int.bit_length.  (Details to be verified.)

--
Terry Jan Reedy

```