There are many algorithms for dungeon generation. This one is my own invention and as such it's not exactly elegant. Its advantage is that it can produce maps that look kind of human-made, because they are they are "partly symmetrical". Here is how it works:
- First we create a "root" room. Every room incl. this one has an origin/entrance.
- Until some end condition is reached (e.g. we've spawned enough rooms), we pick one of the rooms and add symmetrical children to it: two on both sides from the entrance, one on the opposite end from the entrance or both (three children).
- This way we get a symmetrical tree of connected rooms. Perfect symmetry doesn't look good, so occasionally we spawn children rooms unsymmetrically - of different sizes or just one to the side from entrance etc.
- This gives us a "partly symmetrical" map, but it's still a tree and a decent map needs loops, so we add some loops by connecting adjacent room, adding tunnels etc.
That's it. There are some nuances to make it work and more nuances to make it work adequately, but that's the idea.