Where do monsters that wake you up come from in Minecraft?
In Minecraft, are the monsters that wake you up when you sleep in a bed in an unsafe location monsters that already exist and were moved from a dark location, or are they monsters which are spawned given that the game found a path from a dark location?
They are newly spawned monsters that can find a path from a location that supports them spawning.
There is some great research on this by bugi74:
Every time bed(s) become active, all players are gone through one by one, each player given 20 tries of spawning a monster. The normal "monsters do not spawn near player" seems to not apply.
On each try it will roll a random monster type from the subset of skeleton, spider or zombie, and a random start location upto +/-32 blocks in horizontal directions and upto +/-16 blocks vertical from the players sleeping location. Then it seeks downwards from that position for the first opaque block below the current block to be checked. Then it tries if the place is suitable for the monster to spawn (water for water mobs, etc.) but no light level check (yet), seeking upwards if the block is not suitable. (There is a limits of 16 blocks above player position for how high it will seek.) Should a suitable location be found, there is another check for if the mob can spawn there, this time with light level check.
Next the algorithm tries to calculate a path from the found spot to the player. If the last point along the path is less than 1.5blocks away from player's position in all three coordinates, the mob gets spawned on a spot very very near the player (like half a block away) (that's what allows the "warp through walls"), and all kinds of things are set in motion (like waking up and aggro), and no further tries are done for that player that time.
The two cases where the monster gets that close to player are when the path is only blocked by one block thick (thin?) wall and the bed (or suitable section of it) is against that wall, or when it really finds a route to the player. No idea how smart the pathing algorithm is - I don't think it is anything near A* or better, judging how stupid they are when trying to get to player in normal situations (luckily for us!).
I've got a Bukkit plugin that manages sleeping with AFK players that caused me to look in to all this myself.
It is a new mob that spawns to punish you for having a dumb bed location. Easy test;
1) Switch mode to peaceful, this clears out all the monsters
2) Switch back to normal and sleep in an unsafe bed
3) Monster spawns immediately
We can see that this monster is freshly spawned, because time doesn't actually elapse while your sleeping (but instead, the game just sets the time of day to morning), so a mob couldn't have "spawned" followed by "teleported to you". We can witness this lack of time-lapse by noticing that furnaces don't smelt while you're sleeping, for example (this might have been fixed in a recent patch? but I don't think so).