Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.roellig-ltd.de!open-news-network.org!border2.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.026 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'testing,': 0.05; '[0]': 0.07; '0.1)': 0.09; '[1]:': 0.09; 'compression': 0.09; 'def': 0.14; '(2,': 0.16; '(3,': 0.16; '(more': 0.16; 'correlation': 0.16; 'dig': 0.16; 'filename:fname piece:signature': 0.16; 'iterator': 0.16; 'keyerror):': 0.16; 'received:10.13': 0.16; 'running:': 0.16; 'subject:random': 0.16; 'uniformly': 0.16; 'url:general': 0.16; 'wrote:': 0.16; 'case.': 0.18; 'skip:` 10': 0.18; 'test.': 0.18; 'keys': 0.22; 'parameter': 0.22; 'try:': 0.22; 'pass': 0.22; 'wrote': 0.23; 'this:': 0.23; 'cheers,': 0.24; 'header:In-Reply-To:1': 0.24; 'url:edu': 0.24; 'second': 0.24; 'testing': 0.25; 'header:User-Agent:1': 0.26; 'sequence': 0.27; 'looks': 0.29; 'random': 0.29; 'read,': 0.29; 'function': 0.30; 'allows': 0.30; 'certainly': 0.31; 'probably': 0.32; '[1]': 0.32; 'equal': 0.34; 'gives': 0.35; 'minimum': 0.35; 'could': 0.35; 'to:addr:python-list': 0.35; 'fail': 0.35; 'really': 0.35; 'but': 0.36; 'except': 0.36; 'there': 0.36; 'depends': 0.36; 'should': 0.37; 'received:10': 0.37; 'subject:: ': 0.37; 'skip:p 20': 0.38; 'test': 0.39; 'expect': 0.39; 'enough': 0.39; 'to:addr:python.org': 0.39; 'some': 0.40; 'your': 0.60; 'close': 0.61; 'maximum': 0.61; 'simple': 0.61; 'skip:u 10': 0.62; 'more': 0.62; 'skip:n 10': 0.63; '20,': 0.66; 'worth': 0.73; 'ranges': 0.76; '138,': 0.84; 'cecil': 0.84; 'expect.': 0.84; 'jonas': 0.84; 'ruled': 0.84; 'subject:Testing': 0.84; 'westerhof': 0.84; 'url:php': 0.86; 'increases': 0.91; 'look.': 0.91; 'ratio': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wielicki.name; s=k001.sol; t=1433673727; bh=3LbDZOx4hj0Zzs3paKK/cnkGzEqqR8nrPknHSExMHfU=; h=Date:From:To:Subject:References:In-Reply-To; b=mnH+FWWObwNAynTOnTc1m440Tme10Wug3JBb+xSx5b2/ktN8cZULUVIhjVvpBcMMr BAhhn6HLUtLmxD6MOTuvL5fu0uIvP5njTDJSK8yADvmts9oO1TIPRLz3ORIl7bZxVv uMb0kuuvND4a5OJLNJpnQZGupv3RGaVxEY7OMeIbyOzpcpFoN9j6FTmhg0ZtNisOoa LkgizpvuKjAjeYF39Q0AbSVpQvDQBbVbjZ0SBYTe09/F6Wmt2BLsa9rTOe4z3bYDRP w8ulgpTEYlvdAEEmrYmP32HTPlm1elX07HnZaus01O5yZpd7Jflx2tJ2tNt8dwj3Rh KJN10eUfybH/A== Date: Sun, 07 Jun 2015 12:41:53 +0200 From: Jonas Wielicki User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Testing random References: <87oaksowwg.fsf@Equus.decebal.nl> In-Reply-To: <87oaksowwg.fsf@Equus.decebal.nl> Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="1GKjhpMdvVsF9BUc7Btup6Ip1OcNEcs3A" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 110 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433674295 news.xs4all.nl 2860 [2001:888:2000:d::a6]:33096 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92227 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --1GKjhpMdvVsF9BUc7Btup6Ip1OcNEcs3A Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 07.06.2015 08:27, Cecil Westerhof wrote: > I wrote a very simple function to test random: > def test_random(length, multiplier =3D 10000): > number_list =3D length * [0] > for i in range(length * multiplier): > number_list[random.randint(0, length - 1)] +=3D 1 > minimum =3D min(number_list) > maximum =3D max(number_list) > return (minimum, maximum, minimum / maximum) >=20 > When running: > for i in range(1, 7): > print(test_random(100, 10 ** i)) > I get: > (3, 17, 0.17647058823529413) > (73, 127, 0.5748031496062992) > (915, 1086, 0.8425414364640884) > (9723, 10195, 0.9537027954879843) > (99348, 100620, 0.9873583780560524) > (997198, 1002496, 0.9947151908835546) >=20 > and when running: > for i in range(1, 7): > print(test_random(1000, 10 ** i)) > I get: > (2, 20, 0.1) > (69, 138, 0.5) > (908, 1098, 0.8269581056466302) > (9684, 10268, 0.9431242695753799) > (99046, 100979, 0.9808574059953059) > (996923, 1003598, 0.9933489305478886) >=20 > It shows that when the first parameter increases the deviation > increases and when the second parameter increases the deviation > decreases. Exactly what you would expect. But what are the ranges you > would expect with a good random function.=20 Really depends on the number of samples. Appearantly, a good RNG would come close to 1.0 for the ratio. > Then it could be used to test a random function. This is an interesting test (more interesting to me than it looks at the first sight, and certainly better than what I had come up with), but unfortunately, there is more to testing randomness. The test clearly suggests that random functions should have a min/max ratio of about 1.0. Think of a "random" function like this: def fake_random(minv, maxv, _cache=3D{}): try: return next(_cache[minv, maxv]) except (StopIteration, KeyError): iterator =3D iter(range(minv, maxv+1)) _cache[minv, maxv] =3D iterator return next(iterator) (if that is hard to read, I agree; it returns the sequence from minv to maxv (inclusively) over and over again for equal minv and maxv. don=E2=80= =99t pass anything to _cache :), just call it like random.randint) This gives a "perfect" ratio of 1.0, but is not very random. This kind of function would probably be ruled out by a compression or correlation test. If you want to dig deeper into the algorithms for random testing, the dieharder test suite [1] is probably worth a look. It all boils down to the use case. For some use cases, the ``fake_random`` might be good enough (unittesting would be such a case: it is certainly uniformly distributed and allows full coverage of the tested range), for others it would fail catastrophically (don=E2=80=99t g= enerate your cryptographic keys with this!). cheers, Jonas [1]: http://www.phy.duke.edu/~rgb/General/dieharder.php --1GKjhpMdvVsF9BUc7Btup6Ip1OcNEcs3A Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCgAGBQJVdB/8AAoJEMBiAyWXYliKovEP/jxV9xgduieASiL2TyvNzi8T JWjjJRUSHezAhA23NPCvNdN9SP4G6VJGsuzBm2ACNSfHKBQoImSqSEaEt0Dpe/kN uaFvMliREYqu1WbQTFFQoRMuxlt7jri1/E0IOSU89YmhGMWrJu7w72IgDgMVi/1w s14a5yipvs2ONXC9PECI65G3L+22+HeBRnRcrEJx16pPRzh9dyuY87aq2sLi5TkD Qk/0Nf1CKMLgI+oZqgpI5KqnDUBb4RGfHA7OGWAjoRHDCGt/WIarTe6GoVWucCW5 B4wN0bp4pm9eqHz5LpwA15StjXHKj6yF2IbEwlhg+Oi0Z8OkZOqOKGTs3CtItIAr LewNvHL9ZqVUkzIJ6bUTOue5TvqN0egfjF8CfPYuaQqFK+/uCdYgde0MvA5d8bbJ x/Ca7Hzj0kpcXKpnpdYqIJZaaikmRDu43YEJuUit6eUngVqFh2peAqF1GLVGYDnO d+qQAbhCH01C6nVg9qoigYnJz6Mw3nAS/cZFgSzQfpWNZBEYVhC4Zs+1fUxaR+YO klzpZTuTKPEtyHYVIoMJEUxd9hL4tjib0z8tSlFcsYIvdmv/zXr+24dmaLIkv52O qGvhgwVTgXaDLoeth7B0LKzQ0oTOuV4lQPXXwl4iuWY9/XTaSwFLi4UQJ2t75FrQ Puj2vCRZ4pjHWhEuCURE =pqd2 -----END PGP SIGNATURE----- --1GKjhpMdvVsF9BUc7Btup6Ip1OcNEcs3A--