[Tutor] How to test function using random.randint()?

Steven D'Aprano steve at pearwood.info
Sun Mar 20 10:58:53 EDT 2016


On Mon, Mar 21, 2016 at 12:18:01AM +1100, Ben Finney wrote:
> boB Stepp <robertvstepp at gmail.com> writes:
> 
> > On Sat, Mar 19, 2016 at 8:03 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> > > On Sat, Mar 19, 2016 at 04:05:58PM +1100, Ben Finney wrote:
> > >>         if rng is None:
> > >>             rng = random.random
> > >
> > > Typo: you want rng = random.randint.
> 
> No, I meant what I wrote. The ‘rng’ parameter is expected to be bound to
> a RNG. If the caller has not specified a custom RNG instance, we bind
> ‘rng’ to the standard RNG instance found at ‘random.random’.

random.random is not a PRNG instance. It is a bound method:

py> import random
py> random.random
<built-in method random of Random object at 0x869af14>



> > >>         result = rng.randint(1, num_sides)
> > >
> > > And here you just want result = rng(1, num_sides). Otherwise you're
> > > trying to call random.randint.randint.
> >
> > And then this statement would be correct as Ben wrote it.
> 
> Unlike some other examples I have posted, I actually bothered to
> demonstrate this working as expected before posting it :-)

Perhaps you might want to check again?

py> rng = random.random
py> rng.randint(1, 6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 
'randint'



-- 
Steve


More information about the Tutor mailing list