-
Notifications
You must be signed in to change notification settings - Fork 0
/
uniqueRandoms.py
54 lines (39 loc) · 1.2 KB
/
uniqueRandoms.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
import random
import pandas as pd
def genUniqueRandom(samplesize, population):
uniques = set()
assert(samplesize <= population)
while len(uniques) < samplesize:
uniques.add(random.randint(0, population))
return uniques
def closestNumbers(arr: set()):
outs = []
arr = sorted(arr) # return as sorted list
diff = arr[1] - arr[0]
for i in range(len(arr)-1):
if abs(arr[i+1]-arr[i]) < diff:
diff = arr[i+1] - arr[i]
outs = []
outs.append(arr[i])
outs.append(arr[i+1])
elif abs(arr[i+1]-arr[i]) == diff:
outs.append(arr[i])
outs.append(arr[i+1])
# don't do this...
#for i in fungen: ###
# if abs(arr[i+1]-arr[i]) == diff:
# outs.append(arr[i])
# outs.append(arr[i+1])
return [diff, len(outs), (arr[-1]-arr[0])]
draw_size = 200000
population = 40000000
results = []
for i in range(100):
a = genUniqueRandom(draw_size, population)
results.append(closestNumbers(a))
print(f'\rdone with: {i+1}', end='')
# -10 to 10
# -10, -7, -5, -4, -3, 0, 10
# for i in range(1, len(arr)): ###
#
# if abs(arr[i]-arr[i-1]) < diff: