Skip to content

Instantly share code, notes, and snippets.

@amirziai
Created September 25, 2022 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirziai/f3e33572a76b3a89c6ba25b7e2a3ef4c to your computer and use it in GitHub Desktop.
Save amirziai/f3e33572a76b3a89c6ba25b7e2a3ef4c to your computer and use it in GitHub Desktop.
from collections import defaultdict
def compute_slices(
n: int,
k: int,
seed: Optional[int] = None,
) -> List[float]:
np.random.seed(seed)
tokens = np.random.rand(n * k)
servers = np.random.choice(range(n), n * k)
idxs = np.argsort(tokens)
fractions = defaultdict(int)
for idx_prev, idx in zip(idxs, idxs[1:]):
server_prev = servers[idx_prev]
diff = tokens[idx] - tokens[idx_prev]
fractions[server_prev] += diff
last = 1 - tokens[idxs[-1]]
last += tokens[idxs[0]]
fractions[servers[-1]] += last
assert sum(fractions.values()) == 1
return fractions
df_slices = pd.DataFrame(
dict(
k=k,
exp_idx=exp_idx,
server1=compute_slices(n=5, k=k, seed=exp_idx)[0],
)
for k in (1, 5, 10, 100, 1000, 10_000)
for exp_idx in range(100)
).fillna(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment