1001Ferramentas
๐Ÿ‘ฅGenerators

Random Groups Generator

Split a name list into N random balanced groups. Useful for class teams or events.


  

How to randomly split N people into K groups

Dividing a list of N people into K balanced groups at random is a classic problem and shows up in classrooms, hackathons, tournaments and Secret Santa draws. Two ingredients are needed: a fair shuffle of the list, and a deterministic rule to walk the shuffled list and assign each person to a group. The choice of shuffle is what determines whether the distribution is truly uniform.

Fisher-Yates: the right shuffle

The Fisher-Yates shuffle (originally published in 1938 and rewritten in modern form by Richard Durstenfeld in 1964) is the gold standard. In a single linear pass it produces each of the n! permutations with equal probability. The modern in-place version is:

for (let i = arr.length - 1; i > 0; i--) {
  const j = Math.floor(Math.random() * (i + 1));
  [arr[i], arr[j]] = [arr[j], arr[i]];
}

It runs in O(n) time and O(1) extra memory. The naive alternative โ€” arr.sort(() => Math.random() - 0.5) โ€” is biased in several JavaScript engines (V8's TimSort, for example) and also slower at O(n log n). Never use it for anything that needs fairness.

Distributing the remainder when N is not divisible by K

If n = 10 and k = 3, three of the groups cannot all have the same size. The fair rule is round-robin: walk the shuffled list and drop person i into group i % k. Some groups end up with โŒˆn/kโŒ‰ = 4 and the rest with โŒŠn/kโŒ‹ = 3; the extras are distributed evenly across the first n mod k groups. This is what the generator above does.

Common use cases and constraints

  • Classroom โ€” split a 30-student class into pairs or trios for an exercise.
  • Tournament brackets โ€” seed a single-elimination bracket from a shuffled list.
  • Pair programming / code review โ€” random pairs that rotate every sprint.
  • Hackathons โ€” assign random colors, themes or table numbers.
  • Secret Santa โ€” same idea, plus a derangement check so that nobody draws their own name (regenerate or backtrack until satisfied).

Real-world variants often add constraints: do not put persons X and Y in the same group; balance by gender, seniority or skill; cap the size of each group. The simplest implementation is generate-and-test: shuffle, check constraints, reshuffle if violated. For dense constraints, switch to explicit backtracking or constraint propagation.

Round-robin vs random

Round-robin is deterministic โ€” useful when fairness over multiple rounds matters (every player meets every other player exactly once). Random partition gives a single fresh draw and is preferable when the goal is to break habitual groupings. Many tournament formats start with a random seeding and then switch to round-robin or swiss-pairing rounds.

FAQ

Can the same person appear in two groups? No. The algorithm is a partition: each name from the input list is assigned to exactly one group.

What happens if N is smaller than K? Some of the K groups end up empty. The tool will create K groups regardless; the trailing ones simply have zero members.

Is the order inside each group also randomized? Yes โ€” because the shuffle happens once over the entire list before partitioning, members within a group appear in random order too.

Is Math.random() good enough? For social/educational draws, yes. For anything with money, prizes or audit trails, use a cryptographically secure source (crypto.getRandomValues) and ideally a publicly verifiable seed.

Related Tools