doctest random output?
Pavol Lisy
pavol.lisy at gmail.com
Tue Aug 29 09:15:31 EDT 2017
On 8/28/17, Leam Hall <leamhall at gmail.com> wrote:
> On 08/28/2017 11:40 AM, Dennis Lee Bieber wrote:
>
> ... a bunch of good stuff ...
>
> I'm (re-)learning python and just trying make sure my function works.
> Not at the statistical or cryptographic level. :)
>
> Thanks!
>
> Leam
> --
> https://mail.python.org/mailman/listinfo/python-list
>
I am not sure what is best practice but I would use sys.exit to
propagate failure (for better scripting possibility).
For example:
if __name__ == "__main__":
import doctest
import sys
sys.exit(doctest.testmod()[0])
But maybe I am wrong and non zero exit status is just for errors in code?
---
If you don't need something at scientific level (which is hard, see:
https://www.random.org/analysis/ ) you could probably use fact that
random sequences are "hard" to compress. For example something like
this could help ->
>>> import zlib
>>> A = [my_thing() for i in range(100)]
>>> 50 < len(zlib.compress(bytes(A))) < 70
True
But be careful!! Other randint parameters would need other limit values!
# for randint(1,6) you have distribution of lengths like this
collections.Counter(len(zlib.compress(bytes(random.randint(1,6) for i
in range(100)))) for j in range(100000))
Counter({55: 1,
56: 46,
57: 834,
58: 7349,
59: 31035,
60: 42884,
61: 16434,
62: 1397,
63: 20})
# but for randint(1,16) you have distribution like this!
collections.Counter(len(zlib.compress(bytes(random.randint(1,16) for i
in range(100)))) for j in range(100000))
Counter({71: 4,
72: 412,
73: 11291,
74: 27392,
75: 28293,
76: 29103,
77: 3296,
78: 209})
So maybe it help you, maybe not :)
More information about the Python-list
mailing list