Click [See inside] for the tutorial.
Comments are in the project.
Hope this helps! :)
Here's a useful tutorial if you need more explanation:
https://flafla2.github.io/2014/08/09/perlinnoise.html
This is one way you could generate 2D caves for a game like 2D Minecraft (wink wink). The nice thing is that it's very customizable, so the perlin noise can be styled as much as necessary to make it look cave-like.
I'd love to put more time/effort into these tutorials, but I really don't have the time... (both on a daily basis and with the fact that I'm leaving in a few weeks, see profile for more info)