[Python-ideas] A few suggestions for the random module

Sven Marnach sven at marnach.net
Sat Jun 25 20:37:16 CEST 2011

Raymond Hettinger schrieb am Fr, 24. Jun 2011, um 00:02:20 +0200:
>      1. random.expovariate(lambd)
>    This seems reasonable

When I started to prepare a patch, I noticed you already implemented
this one.  Thanks!

>      2. random.sample(population, k)
>    This may be a unnecessary optimization (not worth the complexity),
>    but I will look at it further.

The main point was the generalization to arbitrary iterables, the
reduced memory usage being just a positive side effect.  I'm not
really convinced of the idea any more since it doesn't really fit well
in the current `sample()` implementation.  If someone wants to save
some memory in the case that k is not much less than n, she can just
copy the population, `shuffle()` it and delete the unneeded part.

The only question that remains is if it is worthwhile to introduce a
new function for this purpose.  While there definitely are use cases
for the algorithm, I simply don't know if they are common enough to
justify a function in the standard library.

>      3. random.choice(seq)
>    It could be generalized to arbitrary iterables (Bentley provides an
>    example of how to do this) but it is fragile (i.e. falls apart badly with
>    weak random number generators) and doesn't correspond well with real use
>    cases.

Again, there definitely are real world use cases.  If the above
mentioned function would be introduced, this one would simply be the
special case k = 1.

 -- Sven

More information about the Python-ideas mailing list