How many items are created when a NetHack level is generated?
There are many articles about what items will be generated (see NetHack wiki, e.g.), but I did not find any information about how many items are generated per level.
How many items are created when a NetHack level is generated?
There are many articles about what items will be generated (see NetHack wiki, e.g.), but I did not find any information about how many items are generated per level.
I can't find this on the wiki either. The relevant source code file is mklev.c, function makelevel(). The copy on the wiki is not well annotated, even on the 3.4.3 copy.
I'll try to summarize it, but I haven't touched this file before or any NH source code in years so I may make mistakes.
Standard dungeon generation for the dungeon, i.e. not Gehennom, any special level, the quest, etc will consist of multiple rooms. These rooms may become special rooms which potentially have their own loot, but the wiki does cover this. Monsters may have starting inventory, but these are not random. Hiding monsters may be generated with a random item on top of them.
Within each room, there is a 1-in-(number_of_rooms * 5 / 2) chance of generating a container - a large box 67% of the time, a chest 33% otherwise. The container page mentions the number of items and generation odds. A comment in the source code suggests that every floor has at least a 40% chance of at least one container being created.
If this container is created, it may contain items. Large boxes contain up to 3 items, chests contain up to 5 items. The maximum is then increased by 2 if it is generated locked (80% chance). The container will contain between 0 and (Max) items with equal chances.
Then, there is a 1-in-3 chance of creating a random object somewhere within the room. If that succeeds, there are further 1-in-5 chances of creating another object somewhere in the same room. There's a limit of 100-items-per-room at which point the game will print a warning to screen (Program in disorder! (Saving and reloading may fix this problem.)), but this is likely not ever going to happen.
@Mark in the comments suggests this results in 0.45 random objects per room.
Maze levels - everything in Gehennom and the gnomish mines - do not have rooms and use a different generation function, mkmaz in mkmaze.c. At the very end of the function it will create between 11 and 19 objects somewhere within the walkable space of the maze - these objects have a 50% chance to be a gem, otherwise they're any random item. Additionally, 2-12 boulders are created somewhere within the maze, and 7-13 piles of gold.