[Tutor] 'Common' mistake ... for other newbies

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Apr 12 19:08:23 EDT 2004



On Mon, 12 Apr 2004, David Broadwell wrote:

> > Are they here people who think that such a so-called "mistake" is not
> > a mistake ?
> Not in though but;
>
> > That David programmed  the way he (and we, humans should and
> > that the error lies in language, not in his brain ?
> I DID have an error, I was going the long way around of getting a random
> item from a list.
>
> The function for that is;
>
> random.choice([someindexibleitem])
>
> instead of;
>
> someindexibleitem[random.randint(0,len(someindexibleitem)-1)]

Hi David,


Yes.  Furthermore, random.randint() is deprecated: we shouldn't be using
it if we can help it:

###
>>> help(random.randint)
Help on method randint in module random:

randint(self, a, b) method of random.Random instance
    Return random integer in range [a, b], including both end points.

    (Deprecated; use randrange(a, b+1).)
###


randint() is deprecated precisely because it is prone to one-off errors,
like the one that the original poster ran into.  The original code would
have been fine if random.randrange() were used:

    someindexibleitem[random.randrange(0, len(someindexibleitem))]


Hope this helps!




More information about the Tutor mailing list