Best way to spawn NPCs in random possible locations

As the title says, what’s the best way to spawn NPCs in random possible locations? Avoiding them to spawn in walls.

Have a table of all the random location coordinates you want, then use math.random to select a table key.

That’s not what I’m looking for though. I’ve tried to make an automated way to do that but all I came up with was this inefficient function:
It spawns an NPC somewhere around a specified area, if the NPC is stuck, it will be immediately removed and it will try again til it spawns.

Check if the position is valid BEFORE you spawn it then.

How? :tinfoil:

What I do is checking if it is colliding but how can I do it without spawning it?