commit 0ae04a64193475fa56ee3b5de61ce19e44b2c1a5
parent 475d642541618142bc6647ffc91b09d0ee6cd7da
Author: ThomasV <thomasv@electrum.org>
Date: Fri, 8 Apr 2016 10:34:05 +0200
fix #1703
Diffstat:
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/lib/coinchooser.py b/lib/coinchooser.py
@@ -25,7 +25,6 @@
from collections import defaultdict, namedtuple
from math import floor, log10
-import struct
from bitcoin import sha256, COIN, TYPE_ADDRESS
from transaction import Transaction
@@ -50,22 +49,23 @@ class PRNG:
result, self.pool = self.pool[:n], self.pool[n:]
return result
- def random(self):
- # Returns random double in [0, 1)
- four = self.get_bytes(4)
- return struct.unpack("I", four)[0] / 4294967296.0
-
def randint(self, start, end):
# Returns random integer in [start, end)
- return start + int(self.random() * (end - start))
+ n = end - start
+ r = 0
+ p = 1
+ while p < n:
+ r = self.get_bytes(1)[0] + (r << 8)
+ p = p << 8
+ return start + (r % n)
def choice(self, seq):
- return seq[int(self.random() * len(seq))]
+ return seq[self.randint(0, seq)]
def shuffle(self, x):
for i in reversed(xrange(1, len(x))):
# pick an element in x[:i+1] with which to exchange x[i]
- j = int(self.random() * (i+1))
+ j = self.randint(0, i+1)
x[i], x[j] = x[j], x[i]