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.
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
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,
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.
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
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
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
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
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
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
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
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
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
13 matches
Mail list logo