non sequitur

Post on 14-Apr-2017

30 Views

Category:

Engineering

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Non SequiturAn exploration of Python's random module

Jair Trejo, EuroPython 2014

Jair TrejoDirector of Operations at Vinco Orbis

Random sample

Simulation

552433147702632768969101985048

552433147702632768969101985048

552433147702632768969101985048

552433147702632768969101985048

552433147702632768969101985048

552433147702632768969101985048

552433147702632768969101985048

552433147702632768969101985048

552433147702632768969101985048

552433147702632768969101985048

1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

4.77 Counter({ '9': 14, '2': 12, '8': 12, '3': 11, '4': 10, '6': 9, '1': 8, '0': 8, '5': 8, '7': 8})

1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986

280348253421170679

7638, 3390, 4921, 2162, 6742, 4545, 6570, 1649, 7192, 7248, 5335, 4622, 3628, 1623, 6341, 2082, 3347, 2024, 965, 3122, 7468, 7710, 4441, 7224, 1861, 4633, 4646,

5853, 2576, 6357, 4114, 9249, 5440, 5936, 2360, 5696, 4444, 7491, 1150, 3225, 4006, 480, 3040, 2416, 8370, 569,

2376, 6453, 6412, 1137, 2927, 5673, 1829, 3452, 9163, 9605, 2560, 5536, 6472, 8867, 6236, 8876, 7833, 3558, 6593, 4676, 8649, 8052, 8347, 6724, 2121, 4986, 8601, 9772, 4919, 1965, 8612, 1665, 7722, 6292, 5892, 7156, 2083, 3388, 4785, 8962, 3174, 742, 5056, 5631, 7081, 1405, 9740, 8676, 2729, 4474, 166, 2755, 5900, 8100

7, 23, 41, 59, 89

Python’s random module source code

import random

r = random.Random(seed) r.random()

r.randrange(max) r.random() * max

r.randrange(start, stop)

start + r.randrange(stop - start)

r.randrange(start, stop, step)

r.random() r.randrange(start, stop, step) r.randint(a, b)

r.choice(a_sequence)

r.sample(population, how_many)

r.shuffle(a_list)

sorted(a_list, key=lambda e: r.random())

r.triangular r.gammavariate r.betavariate r.paretovariate r.weibullvariate

import random !

random.random()

random.WichmannHill random.SystemRandom randomdotorg.RandomDotOrg

Conclusions• The definition of randomness is more a philosophical than a mathematical

problem.

• But we can use mathematical definitions that are useful for our purposes.

• If we need sequences that are deterministic, but behave as if random, we can use pseudo-random number generation.

• If we need numbers that are completely unpredictable, we need sources of entropy like input devices, noise measurements or other external sources.

• For most of our random number needs, python provides more than adequate capabilities.

References

10 PRINT CHR$(205.5+RND(1)); : GOTO 10

By Nick Montfort, Patsy Baudoin, John Bell, Ian Bogost, Jeremy Douglass, Mark C. Marino, Michael Mateas,

Casey Reas, Mark Sample and Noah Vawter

The art of computer programming, Volume 2 Seminumerical Algorithms

On randomness in cryptography!http://blog.cloudflare.com/why-randomness-matters

On random number generators testing!http://www.fourmilab.ch/hotbits/statistical_testing/stattest.html

On the possible NSA backdoor into RSA’s random number generator!http://arstechnica.com/security/2014/01/how-the-nsa-may-have-put-a-backdoor-in-rsas-cryptography-a-technical-primer/

Thank you!

top related