On 16 October 2015 at 12:04, Steven D'Aprano <steve@pearwood.info> wrote:
On Fri, Oct 16, 2015 at 08:57:24AM +0200, Victor Stinner wrote:
I don't like the idea how having two functions doing *almost* the same thing: randint() and randrange(). There is a risk that these functions will be misused. I consider that I know some stuff on PRNG but I'm still confused by randint() and randrange(). Usually, I open python and type:
x=[s.randrange(1,6) for n in range(100)] min(x), max(x) (1, 5)
Wouldn't help(randrange) be easier? :-)
Choose a random item from range(start, stop[, step]).
This fixes the problem with randint() which includes the endpoint; in Python this is usually not what you want.
I always find that comment amusing. While it is true that in slicing, half-open ranges are more useful than closed ranges, but when it comes to generating random numbers (say, simulating dice) I find randint much more useful and intuitive.
But I appreciate that some people think differently.
Folks wanting to simulate die rolls should be using the random module rather than the secrets module anyway, so the "only offer secrets.randbelow()" approach Guido suggested in his last email makes sense to me. Regards, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia