Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92227

Re: Testing random

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 <jonas@wielicki.name>
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 <jonas@wielicki.name>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.237.1433674295.13271.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 07.06.2015 08:27, Cecil Westerhof wrote:
> I wrote a very simple function to test random:
>     def test_random(length, multiplier = 10000):
>         number_list = length * [0]
>         for i in range(length * multiplier):
>             number_list[random.randint(0, length - 1)] += 1
>         minimum = min(number_list)
>         maximum = max(number_list)
>         return (minimum, maximum, minimum / maximum)
> 
> 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)
> 
> 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)
> 
> 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. 

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={}):
        try:
            return next(_cache[minv, maxv])
        except (StopIteration, KeyError):
            iterator = iter(range(minv, maxv+1))
            _cache[minv, maxv] = 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’t
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’t generate
your cryptographic keys with this!).

cheers,
Jonas

   [1]: http://www.phy.duke.edu/~rgb/General/dieharder.php


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Testing random Cecil Westerhof <Cecil@decebal.nl> - 2015-06-07 08:27 +0200
  Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-07 12:40 +0200
    Re: Testing random Chris Angelico <rosuav@gmail.com> - 2015-06-07 21:51 +1000
      Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-07 17:51 +0200
        Re: Testing random Chris Angelico <rosuav@gmail.com> - 2015-06-08 02:25 +1000
          Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-07 18:36 +0200
            Re: Testing random Chris Angelico <rosuav@gmail.com> - 2015-06-08 02:44 +1000
              Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-07 20:23 +0200
                Re: Testing random Chris Angelico <rosuav@gmail.com> - 2015-06-08 04:52 +1000
                Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-07 21:41 +0200
                Re: Testing random Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-06-07 22:08 +0300
                Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-07 21:29 +0200
                Re: Testing random random832@fastmail.us - 2015-06-07 15:44 -0400
                Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-07 22:09 +0200
                Re: Testing random random832@fastmail.us - 2015-06-07 16:41 -0400
                Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-07 22:59 +0200
                Re: Testing random Steven D'Aprano <steve@pearwood.info> - 2015-06-08 11:26 +1000
                Re: Testing random random832@fastmail.us - 2015-06-07 21:34 -0400
                Re: Testing random Chris Angelico <rosuav@gmail.com> - 2015-06-08 11:42 +1000
                Re: Testing random MRAB <python@mrabarnett.plus.com> - 2015-06-08 02:49 +0100
                Re: Testing random random832@fastmail.us - 2015-06-07 21:57 -0400
                Re: Testing random Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-06-08 10:40 +0300
                Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-10 19:03 +0200
                Re: Testing random sohcahtoa82@gmail.com - 2015-06-10 10:52 -0700
                Re: Testing random Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-06-10 23:00 +0300
                Re: Testing random Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-10 12:02 -0600
                Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-12 23:32 +0200
                Re: Testing random alister <alister.nospam.ware@ntlworld.com> - 2015-06-12 21:46 +0000
                Re: Testing random random832@fastmail.us - 2015-06-12 17:52 -0400
                Re: Testing random Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-12 16:00 -0600
                Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-13 00:09 +0200
                Re: Testing random sohcahtoa82@gmail.com - 2015-06-12 15:55 -0700
                Re: Testing random random832@fastmail.us - 2015-06-12 18:57 -0400
                Re: Testing random Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-13 08:53 +0100
                Re: Testing random random832@fastmail.us - 2015-06-10 14:26 -0400
                Re: Testing random Ned Batchelder <ned@nedbatchelder.com> - 2015-06-07 14:21 -0700
                Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-16 21:18 +0200
                Re: Testing random random832@fastmail.us - 2015-06-16 16:23 -0400
                Re: Testing random Ned Batchelder <ned@nedbatchelder.com> - 2015-06-16 13:48 -0700
                Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-16 23:57 +0200
                Re: Testing random sohcahtoa82@gmail.com - 2015-06-16 15:30 -0700
                Re: Testing random Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-16 16:58 -0600
                Re: Testing random Laura Creighton <lac@openend.se> - 2015-06-17 11:28 +0200
                Re: Testing random Ned Batchelder <ned@nedbatchelder.com> - 2015-06-16 16:26 -0700
                Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-17 01:45 +0200
                Re: Testing random sohcahtoa82@gmail.com - 2015-06-16 17:36 -0700
                Re: Testing random Chris Angelico <rosuav@gmail.com> - 2015-06-17 11:01 +1000
                Re: Testing random Ethan Furman <ethan@stoneleaf.us> - 2015-06-16 18:32 -0700
                Re: Testing random Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-17 09:41 +0100
                Re: Testing random Grant Edwards <invalid@invalid.invalid> - 2015-06-17 14:04 +0000
                Re: Testing random Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-17 09:01 -0600
                Re: Testing random MRAB <python@mrabarnett.plus.com> - 2015-06-17 01:42 +0100
                Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-17 08:53 +0200
                Re: Testing random Christian Gollwitzer <auriocus@gmx.de> - 2015-06-17 09:22 +0200
                Re: Testing random Chris Angelico <rosuav@gmail.com> - 2015-06-17 17:28 +1000
                Re: Testing random Tim Golden <mail@timgolden.me.uk> - 2015-06-17 08:30 +0100
                Re: Testing random Cecil Westerhof <Cecil@decebal.nl> - 2015-06-17 11:57 +0200
                Re: Testing random Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-06-17 01:36 +0000
                Re: Testing random Laura Creighton <lac@openend.se> - 2015-06-17 12:33 +0200
                Re: Testing random Steven D'Aprano <steve@pearwood.info> - 2015-06-17 22:47 +1000
                Re: Testing random Laura Creighton <lac@openend.se> - 2015-06-17 15:50 +0200
                Re: Testing random Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-06-17 01:35 +0000
                Re: Testing random Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-06-17 07:41 +0300
                Re: Testing random Steven D'Aprano <steve@pearwood.info> - 2015-06-08 11:11 +1000
            Re: Testing random Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-07 11:07 -0600
            Re: Testing random Chris Angelico <rosuav@gmail.com> - 2015-06-08 03:20 +1000
            Re: Testing random "C.D. Reimer" <chris@cdreimer.com> - 2015-06-07 10:36 -0700
            Re: Testing random Steven D'Aprano <steve@pearwood.info> - 2015-06-08 04:28 +1000
              Re: Testing random Chris Angelico <rosuav@gmail.com> - 2015-06-08 04:40 +1000
        Re: Testing random Steven D'Aprano <steve@pearwood.info> - 2015-06-08 04:24 +1000
  Re: Testing random Jonas Wielicki <jonas@wielicki.name> - 2015-06-07 12:41 +0200
  Re: Testing random Steven D'Aprano <steve@pearwood.info> - 2015-06-07 22:52 +1000
    Re: Testing random Steven D'Aprano <steve@pearwood.info> - 2015-06-07 23:06 +1000
    Re: Testing random Peter Otten <__peter__@web.de> - 2015-06-07 15:35 +0200
      Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-07 18:36 +0200
        Re: Testing random Peter Otten <__peter__@web.de> - 2015-06-07 18:48 +0200
          Re: Testing random Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-07 22:15 +0200
      Re: Testing random Steven D'Aprano <steve@pearwood.info> - 2015-06-08 11:35 +1000
  Re: Testing random Christian Gollwitzer <auriocus@gmx.de> - 2015-06-07 14:53 +0200
  Re: Testing random Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-06-07 11:04 -0400

csiph-web