-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrandomDotOrg.py
284 lines (239 loc) · 8.2 KB
/
randomDotOrg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python
# This file is part of RandomSources.
#
# Copyright (C) 2012, Eric Astor
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from math import log as _log, ceil as _ceil, floor as _floor
from random import Random
from urllib import urlencode, quote as urlquote
from urllib2 import Request, urlopen
import functools, sys
bitsPerFloat = sys.float_info.mant_dig
int16 = functools.partial(int, base=16)
class RandomDotOrg(Random):
"""Alternate random number generator using random.org as the
source.
Requires Internet access."""
__version__ = '0.1.0'
_intMin = -1000000000
_intMax = 1000000000
_maxWidth = _intMax - _intMin + 1
_hexMax = int(_floor(_log(_intMax, 16)))
_bitsMax = _hexMax << 2
_bitsMaxInt = 2**_bitsMax-1
_fetchMax = 10000
def _fetch(self, service, **kwargs):
"Fetch data from the Random.org HTTP Interface"
url = 'https://www.random.org/%s/?' % urlquote(service)
options = dict(format='plain')
options.update(kwargs)
headers = {'User-Agent': 'RandomSources.randomDotOrg/%s' % self.__version__}
req = Request(url + urlencode(options), headers=headers)
return urlopen(req).read().splitlines()
def fetchHex(self, digits, rnd='new'):
remainderDigits = digits % self._hexMax
fullInts = digits // self._hexMax
remainderFetch = fullInts % self._fetchMax
fullFetches = fullInts // self._fetchMax
r = ''
options = dict(col=1, base=16, min=0, max=self._bitsMaxInt, num=self._fetchMax, rnd=rnd)
if fullFetches > 0:
r += ''.join(''.join(self._fetch('integers', **options)) for i in xrange(fullFetches))
if remainderFetch > 0:
options['num'] = remainderFetch
r += ''.join(self._fetch('integers', **options))
if remainderDigits > 0:
options['max'] = 16**remainderDigits - 1
options['num'] = 1
r += self._fetch('integers', **options)[0]
return r
def fetchIntegers(self, imin, imax, num, rnd='new'):
fullFetches = num // self._fetchMax
remainderFetch = num % self._fetchMax
r = []
options = dict(col=1, base=10, min=imin, max=imax, num=self._fetchMax, rnd=rnd)
for i in xrange(num // self._fetchMax):
r.extend(map(int, self._fetch('integers', **options)))
if remainderFetch > 0:
options['num'] = remainderFetch
r.extend(map(int, self._fetch('integers', **options)))
return r
def checkBitQuota(self):
return int(self._fetch('quota', format='plain')[0])
def random(self, n=1):
if n == 1:
return self.getrandbits(bitsPerFloat) * 2**-bitsPerFloat
return [x * 2**-bitsPerFloat for x in self.getrandbits(bitsPerFloat, n)]
random.__doc__ = Random.random.__doc__
def getrandbits(self, k, n=1):
"getrandbits(k) -> x. Generates a long int with k random bits."
if k == 0:
return 0
hexString = self.fetchHex((k*n + 3) // 4)
result = long(hexString, 16)
filter = 2**k - 1
if n == 1:
return result & filter
r = []
for i in xrange(n):
r.append(result & filter)
result >>= k
return r
def _stub(self, *args, **kwargs):
"Stub method. Not used for a remote random number generator."
return None
seed = _stub
jumpahead = _stub
def _notimplemented(self, *args, **kwargs):
"Method should not be called for a remote random number generator."
raise NotImplementedError('Remote entropy sources do not have state.')
getstate = _notimplemented
setstate = _notimplemented
## -------------------- integer methods -------------------
def randrange(self, start, stop=None, step=1, n=1):
# This function exactly parallels the code in Random.py,
# with the one additional feature of fetching multiple
# requests together; most comments are copied here.
imin = self._intMin
maxwidth = self._maxWidth
# This code is a bit messy to make it fast for the
# common case while still doing adequate error checking.
istart = int(start)
if istart != start:
raise ValueError('non-integer arg 1 for randrange()')
if stop is None:
if istart > 0:
if istart > maxwidth:
if n > 1:
return self._randbelow(istart, n)
return self._randbelow(istart)
r = self.fetchIntegers(imin, imin + istart - 1, n)
if n == 1:
return r[0]
return r
raise ValueError('empty range for randrange()')
# stop argument supplied.
istop = int(stop)
if istop != stop:
raise ValueError('non-integer stop for randrange()')
width = istop - istart
if step == 1 and width > 0:
if width >= maxwidth:
if n > 1:
return [int(istart + x) for x in self._randbelow(width, n)]
return int(istart + self._randbelow(width))
shift = istart - imin
r = self.fetchIntegers(imin, imin + width - 1, n)
if n == 1:
return shift + r[0]
return [int(shift + x) for x in r]
if step == 1:
raise ValueError('empty range for randrange() (%d,%d, %d)' % (istart, istop, width))
# Non-unit step argument supplied.
istep = int(step)
if istep != step:
raise ValueError('non-integer step for randrange()')
if istep > 0:
size = (width + istep - 1) // istep
elif istep < 0:
size = (width + istep + 1) // istep
else:
raise ValueError('zero step for randrange()')
if size <= 0:
raise ValueError('empty range for randrange()')
if size >= maxwidth:
if n > 1:
return [int(istart + istep*x) for x in self._randbelow(size, n)]
return int(istart + istep*self._randbelow(size))
shift = istart - istep*imin
r = self.fetchIntegers(imin, imin + size - 1, n)
if n == 1:
return int(shift + istep*r[0])
return [int(shift + istep*x) for x in r]
randrange.__doc__ = Random.randrange.__doc__
def randint(self, a, b, n=1):
return self.randrange(a, b+1, n=n)
randint.__doc__ = Random.randint.__doc__
def _randbelow(self, n, num=1, _log=_log, int=int):
k = int(1.00001 + _log(n-1, 2.0)) # 2**k > n-1 > 2**(k-2)
if num == 1:
r = self.getrandbits(k)
while r >= n:
r = self.getrandbits(k)
return r
r = []
filter = (1 << k) - 1
while num > 0:
bits = self.getrandbits(k*num)
for i in xrange(num):
x = bits & filter
if x < n:
r.append(x)
num -= 1
return r
_randbelow.__doc__ = Random._randbelow.__doc__
## -------------------- sequence methods -------------------
def choice(self, seq, n=1):
length = len(seq)
if length == 0:
raise IndexError('list index out of range')
if n == 1:
return seq[self.randrange(length)]
return [seq[i] for i in self.randrange(length, n=n)]
choice.__doc__ = Random.choice.__doc__
def shuffle(self, x, random=None):
if random is not None:
return Random.shuffle(self, x, random)
randrange = self.randrange
for i in reversed(xrange(1, len(x))):
# pick an element in x[:i+1] with which to exchange x[i]
j = randrange(i + 1)
x[i], x[j] = x[j], x[i]
shuffle.__doc__ = Random.shuffle.__doc__
def sample(self, population, k):
# This function exactly parallels the code in Random.py.
# Comments are therefore omitted, to save space.
n = len(population)
if not 0 <= k <= n:
raise ValueError('sample larger than population')
randrange = self.randrange
result = [None] * k
setsize = 21
if k > 5:
setsize += 4 ** _ceil(_log(k * 3, 4))
if n <= setsize or hasattr(population, 'keys'):
pool = list(population)
for i in xrange(k):
j = randrange(n-i)
result[i] = pool[j]
pool[j] = pool[n-i-1]
else:
try:
selected = set()
selected_add = selected.add
for i in xrange(k):
j = randrange(n)
while j in selected:
j = randrange(n)
selected_add(j)
result[i] = population[j]
except (TypeError, KeyError):
if isinstance(population, list):
raise
return self.sample(tuple(population), k)
return result
sample.__doc__ = Random.sample.__doc__
if __name__ == '__main__':
print __doc__.strip()