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

2016-03-20 Thread boB Stepp
On Sun, Mar 20, 2016 at 8:44 PM, Ben Finney wrote: > boB Stepp writes: >> Can I not use: >> >> if rng is None: >> rng = random.Random() > > That will work. > > It unfortunately creates a new random.Random instance every time that > line is executed, making the function waste a lot of time.

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

2016-03-20 Thread Ben Finney
boB Stepp writes: > On Sun, Mar 20, 2016 at 8:19 PM, Ben Finney > wrote: > > if rng is None: > > rng = random._inst > > > > which is the default RNG instance in the module. > > Can I not use: > > if rng is None: > rng = random.Random() That will work. It unfortunately creates

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

2016-03-20 Thread boB Stepp
On Sun, Mar 20, 2016 at 8:19 PM, Ben Finney wrote: > > Steven D'Aprano writes: > > > On Mon, Mar 21, 2016 at 12:18:01AM +1100, Ben Finney wrote: > > > 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,

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

2016-03-20 Thread Ben Finney
Steven D'Aprano writes: > On Mon, Mar 21, 2016 at 12:18:01AM +1100, Ben Finney wrote: > > 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.

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

2016-03-20 Thread Steven D'Aprano
On Mon, Mar 21, 2016 at 12:18:01AM +1100, Ben Finney wrote: > boB Stepp writes: > > > On Sat, Mar 19, 2016 at 8:03 AM, Steven D'Aprano > > wrote: > > > On Sat, Mar 19, 2016 at 04:05:58PM +1100, Ben Finney wrote: > > >> if rng is None: > > >> rng = random.random > > > > > > T

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

2016-03-20 Thread Ben Finney
boB Stepp writes: > On Sat, Mar 19, 2016 at 8:03 AM, Steven D'Aprano 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’ paramete

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

2016-03-19 Thread boB Stepp
On Sat, Mar 19, 2016 at 8:03 AM, Steven D'Aprano 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 n

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

2016-03-19 Thread Alan Gauld
On 19/03/16 13:03, Steven D'Aprano wrote: > But all joking aside, Python's pseudo-random number generator is one of > the best in the world, the Mersenne Twister. For non-cryptographical > purposes, it is as random as anything you are likely to need. > > https://en.wikipedia.org/wiki/Mersenne_T

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

2016-03-19 Thread Steven D'Aprano
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

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

2016-03-19 Thread Steven D'Aprano
On Fri, Mar 18, 2016 at 11:26:13PM -0500, boB Stepp wrote: > If I had a function to roll a die, such as: > > import random > > def roll_die(num_sides): > return random.randint(1, num_sides) > > How would I write unit tests for this? (1) One way would be to monkey-patch the random module wi

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

2016-03-19 Thread Alan Gauld
On 19/03/16 04:26, boB Stepp wrote: > If I had a function to roll a die, such as: > > How would I write unit tests for this? Ben has given the general case answer. > And I do not see how I can test for an appropriate "randomness" to the > numbers the function generates without to a lot of iter

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

2016-03-18 Thread Ben Finney
boB Stepp writes: > If I had a function to roll a die, such as: > > import random > > def roll_die(num_sides): > return random.randint(1, num_sides) > > How would I write unit tests for this? You need to make the system deterministic during the test run. Since the random number generator (RN

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

2016-03-18 Thread boB Stepp
If I had a function to roll a die, such as: import random def roll_die(num_sides): return random.randint(1, num_sides) How would I write unit tests for this? The first thing that suggests itself to me is to do something like assertIn(roll_die(num_sides), range(1, num_sides + 1) And I woul