GenerateRandomSearch

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

  1. Choose how many bracket pairs.
  2. Predict the count before generating.
  3. Compare against the Catalan number shown.
  4. 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.