How long does it take for hostile mobs to spawn in Minecraft?
According to Minecraft Wiki, hostile mobs have a chance of spawning every 1/20th of a second.
Hostile mobs have a spawning cycle once every game tick (1⁄20 of a second). Friendly and water mobs have only one spawning cycle every 400 ticks (20 seconds). Because of this, where conditions permit spawning, hostile mobs spawn frequently, but passive mobs (ie: animals) spawn very rarely. Most animals spawn within chunks when they are generated.
But they have to spawn within 128 blocks of the nearest player.
Mobs spawn naturally within a square group of chunks centered on the player, 15×15 chunks (240×240 blocks). When there are multiple players, mobs can spawn within the given distance of any of them. However, hostile mobs (and some others) that move farther than 128 blocks from the nearest player will instantly despawn, so the mob spawning area is more-or-less limited to spheres with a radius of 128 blocks, centered at each player. In multiplayer, mob caps are shared by all players, no matter where they are.
There also needs to be a light level of 7 or less for hostile mobs to spawn.
Most monsters cannot spawn on peaceful difficulty. At higher difficulty, most of them prevent player from sleep, and most of them spawn at light level of 7 or less.
However, there is also a cap on how many of a single mob can be spawned in an eligible spawning area.
Mob caps are directly proportional to the total number of chunks eligible for spawning. To calculate the cap, the spawning area is expanded by one chunk in every direction (so the default gives 17×17 chunks), then the total number of chunks is plugged into the following formula:
mobCap = constant * chunks / 289. Each mob category has a separate cap and a different constant in the formula:
Hostile = 70 Passive = 10 Ambient (Bats) = 15 Water (Squids) = 5 In single player mode, there are always 289 chunks in range so the caps above are used. In multiplayer, chunks that are in range of multiple players are only counted once. So the more spread out the players are, the more chunks will be covered and the higher the caps will be.
The number of mobs is checked once at the beginning of each spawning cycle against the cap. If the number of living mobs in a category is over its cap, the entire spawning cycle for that category is skipped. The area checked for mobs is the same as the area used for calculating the mob cap, which is the spawning area expanded by one chunk in every direction.