Consider there are n number of balls and bins. For instance, I pick a ball and uniformly random 2 bins everytime which means those bins can be selected later as part of another couple and I need to fill a ball to least occupied bin. How can I do this with numpy array? Thanks in advance.
1
votes
The question is highly unclear, one (or a few) example(s) would be greatly appreciated.
- mozway
For instance, there are 10 balls and bins. I pick the first ball and uniformly random 2 bins, lets say bin 6 and 8 and I will fill ball to least occupied one. In first selection lets say it went to 6 and for the second selection we got bin 6 and 5, in this case the least occupied one is 5 and the ball is going there, by doing so we fill all the balls to the bins.
- Artuk
1 Answers
0
votes
I don't think you can do this in numpy as the process is iterative.
But it's fairly easy with pure python:
import random
n = 10
bins = [[] for i in range(n)]
for ball in range(n):
selected = random.sample(bins, 2)
min(selected, key=len).append(ball)
print(bins)
5 examples of output:
[[6], [], [1], [], [9], [2, 7], [0, 4], [5], [8], [3]]
[[8], [9], [0, 7], [3], [1], [4], [], [], [6], [2, 5]]
[[7], [0, 9], [1], [4, 6], [], [5], [3, 8], [], [2], []]
[[0], [4, 5], [3], [9], [1, 8], [6], [], [2], [], [7]]
[[6, 7], [1], [9], [0], [5, 8], [2], [], [4], [], [3]]