Coverage for kwant/digest.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# Copyright 2013 Kwant authors. # # This file is part of Kwant. It is subject to the license terms in the file # LICENSE.rst found in the top-level directory of this distribution and at # http://kwant-project.org/license. A list of Kwant authors can be found in # the file AUTHORS.rst at the top-level directory of this distribution and at # http://kwant-project.org/authors.
This module provides routines that given some input compute a "random" output that depends on the input in a (cryptographically) intractable way.
This turns out to be very useful when one needs to map some irregular objects to random numbers in a deterministic and reproducible way.
Internally, the md5 hash algorithm is used. The randomness thus generated is good enough to pass the "dieharder" battery of tests: see the function `test` of this module. """
"""Return bytes if the input is a string, else return the object as-is."""
"""Return two independent [0,1)-distributed numbers."""
"""md5-hash `input` and `salt` and map the result to the [0,1) interval.
`input` must be some object that supports the buffer protocol (i.e. a string or a numpy/tinyarray array). `salt` must be a string or a bytes object. """
"""md5-hash `input` and `salt` and return the result as a standard normal distributed variable.
`input` must be some object that supports the buffer protocol (i.e. a string or a numpy/tinyarray array). `salt` must be a string or a bytes object. """ # This uses the Box-Muller transform. Only one of the two results is # computed.
def test(n=20000): # skip coverage """Test the generator with the dieharder suite generating n**2 samples.
Executing this function may take a very long time. """ import os import tempfile import subprocess from tinyarray import array from struct import pack
f = tempfile.NamedTemporaryFile(delete=False) try: for x in range(n): for y in range(n): a = array((x, y)) i = int(2**32 * uniform(a)) f.write(pack('I', i)) f.close() subprocess.call(['dieharder', '-a', '-g', '201', '-f', f.name]) finally: os.remove(f.name) |