1) You could concatenate the two groups and do simple sampling from the combined group, for instance by shuffling the elements and iterating through the shuffled combined set.
2) If you'd rather do it sequentialy you could sample from each group with probabilities size(A) / (size(A) + size(B))
and size(B) / (size(A) + size(B))
, where size(A)
and size(B)
are the current numbers of elements in groups A and B, respectively, that haven't yet been sampled. In other words, if U is a draw from a Uniform(0,1) random number generator:
if U <= size(A) / (size(A) + size(B))
randomly draw next observation from A
else
randomly draw next observation from B
In both approaches the A
's and the B
's end up uniformly distributed across the range, which is the statistical description of a "fairly even distribution".
You didn't specify a language, so here are concrete implementations of both approaches in Ruby. I've cut the set sizes in half to keep the output length reasonable, and obviously these will both produce different results each time they're run due to the use of randomness.
First approach:
a = ['A'] * 21
b = ['B'] * 8
c = (a + b).shuffle
puts c.join(',')
which, for example, produced the following output:
A,A,A,A,A,B,A,A,A,A,A,B,B,B,A,B,A,A,A,A,A,A,A,A,A,B,B,A,B
Second approach:
a = ['A'] * 21
b = ['B'] * 8
c = []
while a.length > 0 || b.length > 0
c << (rand <= (a.length / (a.length + b.length).to_f) ? a.shift : b.shift)
end
puts c.join(',')
which, for example, produced the following output:
A,A,B,A,A,A,B,B,A,A,A,A,A,A,A,B,B,B,A,A,A,B,A,A,A,B,A,A,A