Balanced Bracket Generator
How many ways can n pairs of brackets be arranged so they balance? The answer is the nth Catalan number, and the same sequence counts polygon triangulations, binary tree shapes and lattice paths that never cross the diagonal. Each sequence here is checked by the rule that defines balance: track the depth, never let it go negative, and finish at zero.
What this generator does
Builds every sequence that never closes more brackets than it has opened and finishes level, then counts them against the Catalan number computed by its product formula.
How to use this tool
- Choose how many bracket pairs.
- Predict the count before generating.
- Compare against the Catalan number shown.
- Show the full list for test cases.
Understanding the controls
- How many pairs
- Between 2 and 7. Seven pairs gives 429 sequences, which is the practical limit for listing them.
- Seed
- Chooses which sequence is shown as the example.
- Show the full list
- Lists every balanced sequence, which makes exhaustive test cases for a parser.
Common use cases
- Test cases for a bracket-matching or parser implementation
- Showing where the Catalan numbers come from
- Teaching the running-depth rule for balance
- Exhaustive inputs for a validity checker
- Picking a specific sequence from a seed
How this generator works
Construction never allows a closing bracket without an unmatched opening one, which is why the enumeration produces exactly the balanced sequences and why the count is Catalan. The check re-tests every sequence with a running depth counter and requires the total to match the Catalan number computed independently.
Randomness and fairness
Only which sequence is shown as the example is random; the enumeration and the count are fixed for a given size. Seeded runs pick the same example and are therefore explicitly not cryptographically secure.
For how randomness is produced across the whole site, see how Generate Random works.
Limitations and good to know
- One bracket type only — mixing round, square and curly brackets gives a different and larger count.
- Seven pairs is the ceiling for listing, though the Catalan number itself is computed exactly.
- No invalid sequences are generated, though a parser test suite would want those too.
- Sequences are listed in enumeration order.
- Sequences are discarded on leaving; seed the run for repeatable test cases.
Privacy and your data
Sequences are enumerated in your browser. Nothing is transmitted or kept.
Related generators
- Integer Composition GeneratorEvery ordered way to write a number as a sum, counted by enumeration and by the two-to-the-power formula.
- Set Partition GeneratorEvery way of splitting a small set into unlabelled groups, counted against the Bell number and broken down by group count.
- Binary Search Tree GeneratorAn insertion order and the tree it builds, with all three traversals, the height, and whether it came out balanced.
- Derangement GeneratorPermutations that leave nothing in its original place, counted by enumeration and by recurrence and required to agree.