python - doctest for a function using a randomly generated variable -
i have function compares user input variable randomly generated number using random
module. want write doctest require randomly generated number ignored or overwritten.
in ignorance tried assign value random variable, random number still generated. use random.seed
, if how apply this?
as far can see, "sets" random generator function different starting point rather specifying number replace number have been generated.
the python random number generator generates pseudo-random numbers using deterministic algorithm, based of of seed.
this means if set seed fixed value, can predict numbers module generate:
>>> import random >>> random.seed(1) >>> random.random() 0.13436424411240122 >>> random.random() 0.8474337369372327 >>> random.random() 0.763774618976614 >>> random.seed(1) >>> random.random() 0.13436424411240122 >>> random.random() 0.8474337369372327 >>> random.random() 0.763774618976614
note how 3 'random' numbers repeated after reset seed 1
.
thus, if set seed in doctest, can predict random numbers used module-under-test.
Comments
Post a Comment