From 736da656bc2784a0943f8108578a30f38325e4f9 Mon Sep 17 00:00:00 2001 From: Paco Hope Date: Mon, 3 Sep 2018 23:16:04 +0100 Subject: [PATCH 1/5] Use system random instead of homebrew random --- lib/random.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/random.py b/lib/random.py index d072333..e0e2ec8 100644 --- a/lib/random.py +++ b/lib/random.py @@ -5,33 +5,32 @@ Warning! Don't use this for anything important, it's probably biased ___license___ = "MIT" -# todo: simplify this by using "urandom" -import os +try: + import urandom as sysrand +except ImportError: + import random as sysrand -_bigrand_bytes = 10 -_bigrand_max = pow(256, _bigrand_bytes) +# arbitrary: max is 32-bit +_bigrand_max = pow(2, 32) def _bigrand(): """generates a random number between 0 (incl) and _bigrand_max (excl)""" - base = 0 - for b in os.urandom(_bigrand_bytes): - base = (base << 8) + b - return base + return int(sysrand.getrandbits(32)) def random(): """Return the next random floating point number in the range [0.0, 1.0).""" - return _bigrand() / _bigrand_max + return sysrand.random() def randrange(start, stop=None): """Return a randomly selected element from range(start, stop)""" if stop is None: stop = start start = 0 - return start + (_bigrand() * (stop - start) // _bigrand_max) + return sysrand.randrange(start, stop) def randint(start, stop): """Return a random integer N such that a <= N <= b.""" - return randrange(start, stop + 1) + return sysrand.randint(start, stop) def shuffle(seq): """Shuffle the sequence x in place.""" From 6bad9c700f2c919041079780830c1a94962e8870 Mon Sep 17 00:00:00 2001 From: Paco Hope Date: Mon, 3 Sep 2018 23:40:01 +0100 Subject: [PATCH 2/5] remove unreferenced _bigrand() function --- lib/random.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/random.py b/lib/random.py index e0e2ec8..76c324e 100644 --- a/lib/random.py +++ b/lib/random.py @@ -10,13 +10,6 @@ try: except ImportError: import random as sysrand -# arbitrary: max is 32-bit -_bigrand_max = pow(2, 32) - -def _bigrand(): - """generates a random number between 0 (incl) and _bigrand_max (excl)""" - return int(sysrand.getrandbits(32)) - def random(): """Return the next random floating point number in the range [0.0, 1.0).""" return sysrand.random() From efab551165c46f1571f7fcf775ed84c8573bfca0 Mon Sep 17 00:00:00 2001 From: Paco Hope Date: Wed, 5 Sep 2018 15:37:37 +0100 Subject: [PATCH 3/5] deleted unneeded --- lib/random.py | 40 ---------------------------------------- 1 file changed, 40 deletions(-) delete mode 100644 lib/random.py diff --git a/lib/random.py b/lib/random.py deleted file mode 100644 index e0e2ec8..0000000 --- a/lib/random.py +++ /dev/null @@ -1,40 +0,0 @@ -"""Library to generate random numbers - -Warning! Don't use this for anything important, it's probably biased -""" - -___license___ = "MIT" - -try: - import urandom as sysrand -except ImportError: - import random as sysrand - -# arbitrary: max is 32-bit -_bigrand_max = pow(2, 32) - -def _bigrand(): - """generates a random number between 0 (incl) and _bigrand_max (excl)""" - return int(sysrand.getrandbits(32)) - -def random(): - """Return the next random floating point number in the range [0.0, 1.0).""" - return sysrand.random() - -def randrange(start, stop=None): - """Return a randomly selected element from range(start, stop)""" - if stop is None: - stop = start - start = 0 - return sysrand.randrange(start, stop) - -def randint(start, stop): - """Return a random integer N such that a <= N <= b.""" - return sysrand.randint(start, stop) - -def shuffle(seq): - """Shuffle the sequence x in place.""" - l = len(seq) - for i in range(l): - j = randrange(l) - seq[i], seq[j] = seq[j], seq[i] From 9de689638888699c47841138ab700b7680c681de Mon Sep 17 00:00:00 2001 From: Paco Hope Date: Wed, 5 Sep 2018 15:38:11 +0100 Subject: [PATCH 4/5] removed random dependency, added app import --- snake/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snake/main.py b/snake/main.py index 13761e8..9a29891 100644 --- a/snake/main.py +++ b/snake/main.py @@ -3,9 +3,9 @@ ___name___ = "Snake" ___license___ = "MIT" ___categories___ = ["Games"] -___dependencies___ = ["dialogs", "app", "ugfx_helper", "random", "sleep", "buttons"] +___dependencies___ = ["dialogs", "app", "ugfx_helper", "sleep", "buttons"] -import math, ugfx, ugfx_helper, random, sleep, buttons +import app, math, ugfx, ugfx_helper, random, sleep, buttons from tilda import Buttons ugfx_helper.init() From 2e9aa2e345aaab0d16c57edde206dbb7270179b0 Mon Sep 17 00:00:00 2001 From: Paco Hope Date: Wed, 5 Sep 2018 15:38:33 +0100 Subject: [PATCH 5/5] Removed dependency on random --- 3dspin/main.py | 2 +- breakout/main.py | 2 +- game-of-life/main.py | 2 +- lib/homescreen.py | 2 +- lib/test_random.py | 2 +- screendisco/main.py | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/3dspin/main.py b/3dspin/main.py index 4cff9f8..dd4ab78 100644 --- a/3dspin/main.py +++ b/3dspin/main.py @@ -3,7 +3,7 @@ ___name___ = "3D Spin" ___license___ = "MIT" ___categories___ = ["Demo"] -___dependencies___ = ["app", "ugfx_helper", "random", "sleep", "buttons"] +___dependencies___ = ["app", "ugfx_helper", "sleep", "buttons"] import ugfx from tilda import Buttons diff --git a/breakout/main.py b/breakout/main.py index 5829ec4..ed5447b 100644 --- a/breakout/main.py +++ b/breakout/main.py @@ -3,7 +3,7 @@ ___name___ = "Breakout" ___license___ = "MIT" ___categories___ = ["Games"] -___dependencies___ = ["app", "ugfx_helper", "random", "buttons"] +___dependencies___ = ["app", "ugfx_helper", "buttons"] from tilda import Buttons import ugfx, ugfx_helper, dialogs diff --git a/game-of-life/main.py b/game-of-life/main.py index 7af9a36..bb577b4 100644 --- a/game-of-life/main.py +++ b/game-of-life/main.py @@ -3,7 +3,7 @@ ___name___ = "Conway game of life" ___license___ = "MIT" ___categories___ = ["Games"] -___dependencies___ = ["app", "ugfx_helper", "random", "sleep", "buttons"] +___dependencies___ = ["app", "ugfx_helper", "sleep", "buttons"] import app, ugfx, ugfx_helper, buttons, sleep, time, random from tilda import Buttons diff --git a/lib/homescreen.py b/lib/homescreen.py index 8702735..5afa460 100644 --- a/lib/homescreen.py +++ b/lib/homescreen.py @@ -17,7 +17,7 @@ They also *may*: """ ___license___ = "MIT" -___dependencies___ = ["database", "buttons", "random", "app", "sleep", "ugfx_helper", "wifi", "sim800"] +___dependencies___ = ["database", "buttons", "app", "sleep", "ugfx_helper", "wifi", "sim800"] import database, ugfx, random, buttons, tilda, sleep, ugfx_helper, wifi, time, sim800 from app import App diff --git a/lib/test_random.py b/lib/test_random.py index 5402caa..6c60dad 100644 --- a/lib/test_random.py +++ b/lib/test_random.py @@ -1,7 +1,7 @@ """Tests for random lib""" ___license___ = "MIT" -___dependencies___ = ["upip:unittest", "random"] +___dependencies___ = ["upip:unittest"] import unittest from random import * diff --git a/screendisco/main.py b/screendisco/main.py index 0615914..5fa8dce 100644 --- a/screendisco/main.py +++ b/screendisco/main.py @@ -3,7 +3,7 @@ """ ___name___ = "Screen Party" ___license___ = "MIT" -___dependencies___ = ["ugfx_helper", "sleep", "random"] +___dependencies___ = ["ugfx_helper", "sleep"] ___categories___ = ["Homescreens"] ___bootstrapped___ = False