[Tutor] How to test function using random.randint()?
boB Stepp
robertvstepp at gmail.com
Sun Mar 20 00:12:13 EDT 2016
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:
>
>> import random
>>
>> def roll_die(num_sides, rng=None):
>> """ Return a result from a random die of `num_sides` sides.
>>
>> :param num_sides: The number of sides on the die.
>> :param rng: An instance of `random.Random`, the random
>> number generator to use.
>> Default: the `random.random` instance.
>> :return: The integer result from the die roll.
>>
>> The die is defined to have equal-probability faces, numbered
>> from 1 to `num_sides`.
>>
>> """
>> if rng is None:
>> rng = random.random
>
> Typo: you want rng = random.randint.
I am thinking that Ben meant: rng = random.Random(). This would then
make sense in light of the test cases he sets up later.
>
>> 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. Ben, please
chime in if I have got it wrong.
boB
More information about the Tutor
mailing list