The numbers are assigned in order to items of specific type. So a pistol gets it's own number series, and a rifle it's own, and a carbine it's own, and a wheeled vehicle it's own, and a tracked vehicle it's own. So you can have many different items with the same number, they just won't be the same type item.
An example of this would be in WWII. The M1 Rifle, The M1 Thompson SMG, The M1 carbine, the M1 "steel pot", the M1 ammo can (.30cal), and so on. We bought so much new stuff at once for WWII, that there was alot of "M1" during that period.
Before WWII we used year of adoption. That's why you have M1911A1 pistol, or M1903A1 rifle. It gets confusing though when you buy more than one thing. for example the M1917 revolvers were two entirely different guns, and today would have different numbers, but because we used year of adoption they were designated the same.
So when the powers that be saw the war coming, and what it would require, they changed to the "M" system of the time.
Today's system is a more refined and evolved version of that one, though there's alot of inconistency between the two due to changes and time.
"M" means model
"X" is a prefix used to designate an experimental model
"E" is a suffix used to designate an experimental variant
"A" is a suffix used to designate an adopted variant.
So an XM4 Carbine is the experimental model of the 4th carbine. When it's adopten the "X" is dropped and it becomes the "M4 carbine". It's the 4th carbine because the Army simply hasn't messed with a carbine sine the M3. So the next one is M4.
The M16A1E1 was the first experimental variation of the first adopted variation of rifle 16. There's a big void between M1 and M14 that can't really be explained, other than saying the system changed during that time and that's just how it ended up. From M14 on though, the system is constant. There was an M15 and then an M16. Anyway, once adopted, the M16A1E1 became the M16A2. The second adopted variation of the model.
During experiments and trials, if there's a possibility that the weapon will be adopted, then it's assigned a number. So not everything that gets tested gets a number. Just stuff that might go the distance. So when the Army tests several things, like for the SAW, you get the XM249, and the XM248(Ford Areospace), XM262 (HK), XM106 (souped up M16) contending. When the M249 got adopted, the others drop by the wayside and those numbers aren't used. That's why you get gaps in the system from one number to the next serving number. In between the two numbers in use, there was an experimental that didn't make it. This allows the Army to go back and resurrect something if it turns out it's needed wihtout alot of confusion.
There are inconsistancies of course, as this is the Army we're talking about. The M9 pistol was numbered "M9" regardless of the model. They were all "XM9". So you get weird stuff alot.
Tracked vehicles run on the same number series. So you have an M1 tank, an M2 Brad, an M3 Brad, an M4 Brad, and on. The M8 AGS was the XM8 in testing. And so on.
Wheeled vehicles have their own. Which is why you have M1008 and so on.
The assignment of the number is done by the proponent agency within the Army that's responsible for that piece of equipment. Even if a number is assigned incorrectly, the Army will keep that number and use it unless it's going to cause alot of confusion. The reason being is that it actually costs millions to change the number of a weapons program once it's going. Changing everything over to a different number costs alot of money, and is why the "X" is used. Parts, publications, and all logistics support remains the same and even old stuff with "XM" on it can be used safely with minimal confusion. If you had an M4 carbine with an "XM4 Carbine" manual, it wouldn't be so confusing that you couldn't use them. If you had an M4 Carbine, but a maual with "T556" on it, it could be very confusing at the troop level.
Navy stuff is bought with the "MK" designation in place of "M". "Mod" is roughly the same as "A". A MK19Mod3 40mm was originally developed by the Navy for river boats in Vietnam, and that's why it's still the "MK19" today, even when used by the Army. The proponent service names it, and eveyone else uses the same name to prevent confusion. The Navy handled the MK23 pistol, even though if used in the Army it will still be "MK23".