Most efficient way to dynamically create a maze

I am creating a game, where a troll can run around in a maze to find a cheese cake!* (fun fun fun!)*
So I was thinking about how I should make this maze, it’s supposed to be 20x20 blocks.

My first thought was to use a function that will randomize out the blocks, and later use A-Star algorithm to find out if the maze is possible to solve or not.
If not, it will randomize another map and try again.

However, my second thought was that this might put the game into a “endless” loop if you are unlucky because the chance that the randomized maze actually is possible to be solved is rather small.

So my question to you is:
How would you create a 20x20 block maze?

Thanks in advance,
~Tompa